Add site column to content type programmatically in SharePoint

This SharePoint tutorial explains, how to add an existing site column to a content type using the JavaScript client object model (jsom) in SharePoint 2013/2016 or SharePoint Online.

A content type is an entity that uses site columns to define the schema of fields for an item is a list or document library. We can reuse content type across multiple lists or document libraries in SharePoint 2013/2016.

Read Create Site Column and Content Type using PowerShell in SharePoint 2013/2016

By default, SharePoint provides various content types that are available in the content type gallery in the site collection. It can be accessible under the site settings page.

Create Content Type in SharePoint 2013

We can create content type simply by using a browser as well as you can create content type by using Visual Studio 2013. Here we will discuss how we can create a content type by using a browser.

Open your SharePoint site collection, from the settings icon, click on Site Settings. Then in the Site Settings page, click on Site content types under “Web Designer Galleries”.

This will open the Site Content Types page, here click on Create link in the top. This will open the New Site Content Type. Give a Name for the content type, then choose the parent content type which looks like below:

Add site column to content type programmatically in SharePoint

Here I have created a content type name as MyCompany.

By default Title column will be added to the content type. Then I have added two more site columns (by clicking on Add from new site column link) to the content type. So by default, it should look like below:

add site columns to content type programmatically

Then I have created another site column known as “Company Branches”. Now here we will try to add this site column to the content type by using the JavaScript object model.

Add Site Column to Content Type using JavaScript Object Model

Below is the JavaScript code to add an existing site column to content type using JavaScript Object Model in SharePoint Online or SharePoint 2013/2016.

Just create a web part page, and then edit that web part page and add a script editor web part.

Insert the below JavaScript code to the script editor web part. Save the page.

<script language="javascript" type="text/javascript">
function AddContentType() {
var ctx = new SP.ClientContext.get_current();
var field = ctx.get_site().get_rootWeb().get_fields() .getByInternalNameOrTitle("Company_x0020_Branches");
var ctype = ctx.get_site().get_rootWeb().get_contentTypes() .getByI("0x0100BD927BBDD704954CB6FEA329CB809491″);
ctx.load(ctype);
ctx.load(field);
var createInfo = new SP.FieldLinkCreationInformation();
createInfo.set_field(field);
var fieldLink = ctype.get_fieldLinks().add(createInfo);
ctype.update (true);
ctx.load(fieldLink);
ctx.executeQueryAsync(success, failure);
}

function success() {
alert('success');
}

function failure() {
alert('failure');
}
</script>

<button type="button" onclick="AddContentType();">Add Site Column to Content Type</button>

Here you need to give the Site column’s internal name. And The GUID of the content type.

Click on the button, it will add the site column to the content type.

Now if you will open the content type, you will be able to see the site column added to the content type.

add site columns to content type programmatically sharepoint

You may like following SharePoint content type tutorials:

Here, we learned how to create a content type in SharePoint and how to add site columns to content type programmatically in SharePoint.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>