.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

Change ContentType of SPListItem by code

SharePoint 2007

Category SharePoint 2007  | Publication Date : 11/7/2009

The user interface of SharePoint allows you to select or change the content type of a list item. How to do it by code?

The class SPListItem 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 aListItem, SPContentType aContentType)
{
    aListItem["ContentTypeId"] = aContentType.Id;
    aListItem.Update();
}