views:

361

answers:

1

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();
             }
          });
+2  A: 

1) Set SPWeb.AllowUnsafeUpdates = true
2) You may need to validate the FormDigest with ValidateFormDigest

SPSecurity.RunWithElevatedPrivileges(() =>
{
    using (SPWeb thisWeb = site.OpenWeb(webUrl))
    {  
        try
        {
         thisWeb.AllowUnsafeUpdates = true;

         if (!thisWeb.ValidateFormDigest())
             throw new InvalidOperationException("Form Digest not valid");

         thisWeb.Title = newName;
         thisWeb.Update();
        }
        finally
        {
            if(thisWeb != null)
                thisWeb.AllowUnsafeUpdates = false;
        }
    }
});
Jon Schoning
+1 You also should wrap this in a try/finally which sets thisWeb.AllowUnsafeUpdates = false; at the end.
Chris Ballance
Thanks, updated to include finally block as suggested
Jon Schoning