| Subcribe via RSS

Change SharePoint ContentType of ListItem

May 4th, 2008 Posted in .NET, SharePoint

The user interface of SharePoint allows you to select or change the ContentType of a list item. I asked myself how this could be done by code. It wasn’t as easy as I thought.

The class ListItem has a property ContentType but it’s readonly. I had to reflect the Microsoft.SharePoint.dll and finally used this workaround to change the content type:

void SetContentType (SPListItem item, SPContentType ct)
  item["ContentTypeId"] = ct.Id;

In addition I wanted to set the ContentType of ListItems to their parent content type. Setting the content type to the type of the parent content type doesn’t change anything because item.ContentType.Parent and item.ContentType are equal. So I tried the parent of the parent and it worked:

void SetContentTypeToParentType (SPListItem item)
  item["ContentTypeId"] = item.ContentType.Parent.Parent.Id;

One Response to “Change SharePoint ContentType of ListItem”

  1. Kay-Christian Wessel Says:


    This worked perfect and you saved my day(night).

Leave a Reply