0

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();
             }
          });
1
  • 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 Commented Nov 5, 2009 at 16:47

2 Answers 2

4

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(); 
    }
  }
3
1

Or do elevate and set AllowUnsafeUpdates = true on your web

2
  • I haven't found that to always work. ValidateFormDigest seems more reliable.
    – Alex Angas
    Commented Nov 5, 2009 at 19:25
  • Really? I've never had any problems using it Commented Nov 6, 2009 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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