Pages

Search This Blog

Monday, May 2, 2011

Updating or Changing Content Type of SPListItem through Code in Sharepoint

Through User Interface(UI) Sharepoint allows us to change the Content Type of a list Item.
But to achieve the same thing through coding is little tricky, because SPListItem have a property named "ContentType" but that property is ReadOnly.

//Retrieved form SPListItem class
public SPContentType ContentType { get; }

This means we can not set this property. This is only for retrieval purpose.

Then, how to achieve our requirement.

To achieve this, I have written a method, that will definitely helps you to resolve this issue.

public void SetContentType(SPListItem objSPListItem, SPContentType objSPContentType)
{
objSPListItem[SPBuiltInFieldId.ContentType] = objSPContentType.Id;
objSPListItem.Update();
}

This method takes two arguments: 1. object of SPListItem and 2. object of SPContentType you want to associate with that item.

By using this method you can easily able to change Content Type of a SPListItem.


No comments:

Post a Comment