Update Content Type including children

Recently I have been doing some work with Content Types via the SharePoint object model and as well as the read only issue which I have also posted about I ran into a security issue.  Whenever attempting to update a content type and propagate all children I would receive the following error:

“The security validation for this page is invalid”

This one took quite a while for me to get to the bottom of and as it turned out all that was required to fix it was disabling security validation by setting the .FormDigestSettings property before the content type update statement as discussed here.

Having read the above I proceeded to

modify my code as follows:
SPSecurity.RunWithElevatedPrivileges(delegate()
{

    using (SPSite site = new SPSite(strSite))
    {
       
site.AllowUnsafeUpdates = true;
       
using (SPWeb web = site.OpenWeb())
       
{
            
// update will fail unless security validation is disabled
            SPWebApplication webApp = web.Site.WebApplication;
            webApp.FormDigestSettings.Enabled = false;
           
 web.AllowUnsafeUpdates = true;            // code goes here
           
            // update including children
           
contentType.Update(true
);

            web.AllowUnsafeUpdates = false;
            // re-enable security validation
            webApp.FormDigestSettings.Enabled = true;
        }
        site.AllowUnsafeUpdates =
false;
    }
});

By disabling the .FormDigestSettings prior to the update combined with allowing unsafe updates I was able to propagate content type changes to children without receiving any errors.  Hooray!

Advertisements
This entry was posted in SharePoint and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s