Add Content Type to List using Rest API in SharePoint Online Office 365

Add Content Type to List using Rest API in SharePoint Online Office 365, SharePoint 2013 add content type to list using rest api
InfoPath alternatives for form designing SharePoint

Introduction:
Here we will discuss how we can add 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.

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.

You can also read some userful articles below:
– Microsoft Flow Send approval email when a new item is added demo

– How to retrieve host web site title in add-in web in SharePoint hosted add-in in SharePoint Online Office 365 Site?

– Office 365 Document Deletion Policies in Office 365 SharePoint Online and how to create and implement document deletion policy in Site Collection

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 sttings -> 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 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 List using Rest API in SharePoint Online Office 365
Add Content Type to List using Rest API in SharePoint Online Office 365

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

Add Content Type to List using Rest API in SharePoint Online Office 365, SharePoint 2013 add content type to list using rest api
Add Content Type to List using Rest API in SharePoint Online Office 365, SharePoint 2013 add content type to list using rest api

Conclusion:
Here we have discussed how we can add an existing content type to a custom list in SharePoint online using javascript object model (jsom).

Similar SharePoint 2013 Tutorials

About 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

View all posts by Bijay Kumar →

Leave a Reply