Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When attempting an SPWeb rename I receive the following SPException:

Exception SPException - The security validation for this page is invalid.  Click Back in your Web browser, refresh the page, and try your operation again. - Failed to create workgroup registration entry

Any idea what might be the troubles here? Here is the relevant code:

SPSecurity.RunWithElevatedPrivileges(() =>
         {
             using (SPWeb thisWeb = site.OpenWeb(webUrl))
             {  
                 thisWeb.Title = newName;
                 thisWeb.Update();
             }
          });
share|improve this question
    
a bit off-topic, but just wanted to let you know that it is important that you reinstantiate you SPSite object within a RunWithElevatedPrivileges block to ensure your objects are 100% elevated –  Jaap Vossers Nov 5 '09 at 16:47
add comment

2 Answers

First, don't elevate, impersonate!

Second, you need to have SharePoint validate the security digest before changing the context:

  Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest();

  SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;

  if (sysToken == null)
  {
    SPSecurity.RunWithElevatedPrivileges(
      delegate()
      {
        using (SPSite site = new SPSite(webUrl))
        {
          sysToken = site.SystemAccount.UserToken;
        }
      }
    );

  }

  using (SPSite site = new SPSite(webUrl, sysToken))
  {
    using (SPWeb web = site.OpenWeb())
    {
      thisWeb.Title = newName;
      thisWeb.Update(); 
    }
  }
share|improve this answer
1  
+1 for ValidateFormDigest. –  Alex Angas Nov 5 '09 at 9:31
    
Why Impersonation over Elevation? –  Jaap Vossers Nov 5 '09 at 16:48
1  
@Jaap: There's info on that here - sharepointoverflow.com/questions/18/… –  Alex Angas Nov 5 '09 at 19:26
add comment

Or do elevate and set AllowUnsafeUpdates = true on your web

share|improve this answer
    
I haven't found that to always work. ValidateFormDigest seems more reliable. –  Alex Angas Nov 5 '09 at 19:25
    
Really? I've never had any problems using it –  Jaap Vossers Nov 6 '09 at 9:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.