// you’re reading...

SharePoint

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.

You might be interested in:
  • SharePoint 2007 Test Data Population Tool
  • SHAREPOINT DESIGNER 2007 IS NOW FREE!
  • Set the SharePoint Welcome Page through code
  • Programmatically Delete all items from a SharePoint List
  • SharePoint - Add IntelliSense when editing CAML files in WSS v3 / MOSS 2007
  • Discussion

    No comments for “SharePoint - Add or Delete Site Column reference from Content Types”

    Post a comment

    Spam Protection by WP-SpamFree

      Locations of visitors to this page
    View Praveen Modi's profile on LinkedIn

    Recent Comments

    • D Baldassarri: Hello Praveen I apologize if this is a reduntant question. I have a List, when the list is created I...
    • Tanuj Kumar: Check this helpful link…. http://mindstick.com/Articles/ 19b8cf75-6d2e-4504-8840-718...
    • sanjeev: Hello i want to upadate all the items of a column.I need to apply one formula for upadating the...
    • pankaj: Thanks a lot.Though i m already done with it.will try this too.
    • Sandeep: Here’s correct way to do it http://snahta.blogspot.com/201 1/09/deleting-all-items-from-l ists.html