Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > operation is not valid due to the current state of the object
September 03
operation is not valid due to the current state of the object

I got this error while running this code in a custom webservice :

try
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
       {
           SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
           webApplication.Prefixes.Add("blogs", SPPrefixType.WildcardInclusion);
           webApplication.Update(true);
       });    
}

The problem is that the webservice is not really running in the SharePoint context and thus the whole SPSecurity.RunWithElevatedPrivileges does not work. That’s why you receive the ‘operation is not valid due to the current state of the object’ error.  It’s easily solved by removing the function like this :

try
{    
   SPWebApplication webApplication = SPWebApplication.Lookup(new Uri(webApplicationUrl));
   webApplication.Prefixes.Add("blogs", SPPrefixType.WildcardInclusion);
   webApplication.Update(true); 
}

Meaning that whenever you call this type of methods in a webservice, make sure that the logged on user has the proper credentials to do this type of actions.

Hope this helps someone ;)

Comments

Krishna Prasad

Hi Robin,
 Your blog seems to be useful.But, i have a question . We have a class that adds the user to the  
Sharepoint site,for which we are using RunWithElevatedPrivileges.We are calling  
this method through a web service.
Inspite of not using 
RunWithElevatedPrivileges in web services,we are getting the same error.Could you please,post your thoughts on this ??
Regards,
Krishna Prasad H
System Account on 03/02/2009 05:03

Jegan.J

Hi ,

   I am also facing the same problem. Could any post ideas about it.

Regards
Jegan.J

System Account on 16/04/2009 01:52

Robin

Are you calling the webservice from within the SharePoint context ?
System Account on 22/04/2009 03:51

Hello I'm getting same error. I can't get access to my sharepoint site and when I try to deploy the project I get the following error: operation is not valid due to the current state of the object

please help i don't know what to do!
System Account on 25/08/2009 05:15
 

 Statistics

 
Views: 4547
Comments: 4
Tags:
Published:1644 Days Ago