// you’re reading...

SharePoint

SharePoint - Add or Delete Site Column reference from Content Types

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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 Plugin

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

    Recent Comments

    • Praveen Modi: Juan, seems like it should work but I haven’t tried it. I will give it a try and will update the...
    • Juan Larios: couldn’t you just do: SPFolderCollection collection = site.Lists["Site Collection...
    • sara: Try this too, Delete item from SharePoint List and more
    • jayashri kanungo: The video is slow. Usually when you run it for first time, it takes time but for subsequent time it...
    • Robert Woodlock: Thank you, sorry to ask, but would i add code to have this function automated.. Basically I was to...