Add Content Type to List using Rest API in SharePoint Online/2013/2016

This Rest API SharePoint tutorial explains, how to add a content type to list using Rest API in SharePoint Online Office 365. The same rest API code will also work in SharePoint 2013 as well as SharePoint 2016.

Add a content type to SharePoint List using Rest API

In this example let us take a button and on click on the button, we will add the reservation content type to a custom list which is there in the site. Both the HTML code and the rest API code let us put inside a script editor web part which is there inside a web part page in SharePoint.

HTML Code:

<div>
<input type="button" id="btnSubmit" value="Add Content Type to List" />
</div>
<div id="divResults"></div>

Rest API Code:

Here if you will look at the code, we are adding a content type id like below:
“contentTypeId”: “0x0102004F51EFDEA49C49668EF9C6744C8CF87D”

In this particular example, the ID is the id of a reservation content type. To get any content type id, you can go to the site settings -> then Site Content Type. Then you can click on any of the content type and from the browser, you can get the content type id. You will see like below:

?ctype=0x0102004F51EFDEA49C49668EF9C6744C8CF87D

Below is the full rest api code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>

$(function () {
bindButtonClick();
});

function bindButtonClick() {
$("#btnSubmit").on("click", function () {
addContentType();
});
}

function addContentType() {
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('MyCompanyInfo')/ContentTypes/AddAvailableContentType";
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
"contentTypeId": "0x0102004F51EFDEA49C49668EF9C6744C8CF87D"
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
}

function onQuerySucceeded() {
$("#divResults").html("Content Type Added Successfully to List !!!");
}

function onQueryFailed() {
alert('Error!');
}
</script>

Once you Save the web part page, you can see a button will appear and on button click, it will add the particular content type to the list which looks like below:

add content type to sharepoint list using rest api

Now if you will open the SharePoint Online list, you can see the content type like below:

add content type to sharepoint online list using rest api

You may like following SharePoint Rest API tutorials:

This SharePoint tutorial explains, how we to add an existing content type to a custom list in SharePoint Online using Rest API. We can also use the same code to add a content type to SharePoint list using Rest API in SharePoint 2013/2016.

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

  • […] Add Content-Type to List using Rest API in SharePoint Online/2013/2016 […]

  • […] Add Content Type to List using Rest API in SharePoint Online/2013/2016 […]

  • >