Updating Content Types

I was having a problem recently where I was unable to issue an update command on a content type without running into the following error:

“The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.”

After much head scratching, web browsing and trial and error I discovered that when retrieving my content type collection I was inadvertently getting a read only collection.  This was not particularly clear from the documentation, but there in fact two methods available for retrieving a collection of content types and these are web.AvailableContentTypes and web.ContentTypes.  The key difference is that web.AvailableContentTypes is a read only collection and therefore fails when an update is issued.

I amended my code to use web.ContentTypes as follows and it worked as expected:

// get content type using writeable content type collection
SPContentType
contentType = web.ContentTypes[“CONTENT_TYPE_NAME”];

// get field to remove from content type
SPField
field = web.Fields[“FIELD_NAME”];
SPFieldLink fieldLink = new SPFieldLink(field);

// delete field from content type by field id
contentType.FieldLinks.Delete(fieldLink .Id);

// update
contentType.Update(); 

These links helped me to diagnose this issue: 

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

One Response to Updating Content Types

  1. Pingback: Update Content Type including children « SharePoint malarkey…

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