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

Or do elevate and set AllowUnsafeUpdates = true on your web

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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