| 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 ;) |