Musings on SharePoint, Adobe CQ, ECM, and more…

SharePoint – Add or Delete Site Column reference from Content Types

Content type does not contain a column, or field. It only contains a reference to a site column or field. Therefore you should create a site column before you can add it to the content type definition.

The field reference in content type are managed through SPFieldLink object.

Add field reference from Content Type:

public static void AddFieldRefToContentType(SPContentType contentType, SPField field)
{
    //Check if the Field reference does not exists already 
    if (!contentType.Fields.ContainsField(field.Title))
    {
         contentType.FieldLinks.Add(new SPFieldLink(field));
         contentType.Update();
    }
    else
    {
         //Do Nothing
    }
}

Delete field reference from Content Type:

public static void DeleteFieldRefFromContentType(SPContentType contentType, SPField field)
{
    //Check if the Field reference exists
    if (contentType.Fields.ContainsField(field.Title))
    {
         contentType.FieldLinks.Delete(field.Title);
         contentType.Update();
    }
    else
    {
         //Do Nothing
    }	
}

Check MSDN information for more details on Field and Field References.

Praveen Modi

Praveen works at Razorfish which is one of the largest digital advertising agency in the world. His mantra for life is "You are never too old to set another goal or to dream a new dream". He lives in sunny Austin, TX with his beautiful wife Nidhi and son Aariv.

Leave a Reply

This blog is kept spam free by WP-SpamFree.