// 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.

SharePoint – Add or Delete Site Column reference from Content Types

You might be interested in:

Discussion

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

Post a comment

AWS Certified Solutions Architect
    Locations of visitors to this page
View Praveen Modi's profile on LinkedIn

Recent Comments


    Warning: Use of undefined constant comments - assumed 'comments' (this will throw an Error in a future version of PHP) in /home/customer/www/praveenmodi.com/public_html/wp-content/plugins/get-recent-comments/get-recent-comments.php on line 928

    Warning: Use of undefined constant trackbacks - assumed 'trackbacks' (this will throw an Error in a future version of PHP) in /home/customer/www/praveenmodi.com/public_html/wp-content/plugins/get-recent-comments/get-recent-comments.php on line 929