Posted by: R Manimaran | August 4, 2014

SharePoint 2013 REST API: Associate an existing content Type to a SharePoint List

SharePoint 2013: REST API: Associate an existing content Type to a SharePoint List

I have created a List using the SharePoint REST API, but when I tried to associate an existing Site Content type to this list I found difficult in doing that. Below is the endpoint and method I have used to associate my content type.

Resource End Point Url: /_api/web/lists/getbytitle(‘<listname>’)/ContentTypes

Method Name: AddAvailableContentType

I have used the Jquery to do the REST operations. Below is the format.

$.ajax(
{
'url':'endpointUrl',
'method':'POST',
'data':JSON.stringify({
    'contentTypeId':null
}),
'headers':{
     'accept':'application/json;odata=verbose',
     'content-type':'application/json;odata=verbose',
     'X-RequestDigest':$('#__REQUESTDIGEST').val()
     },
'success':function (data) {
   var d = data;
},
'error':function (err) {
   alert(JSON.stringify(err));
}
});

We need to pass the Content Type id to get associated with the List. Below is the example I have used to associate Employees content Type to Employees List.

 

function addContentType() {
var siteUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Employees')/ContentTypes/AddAvailableContentType";
var call = jQuery.ajax({
    url: siteUrl,
    type: "POST",
    data: JSON.stringify({
	  "contentTypeId": "0x0100E5EC1FE6D284A74A972A1776FFFE2DA0"
          }),
    headers:
    {
	'accept': 'application/json;odata=verbose',
	"content-type": "application/json;odata=verbose",
	"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
    }
   });

call.done(function (data, textStatus, jqXHR) {
var message = jQuery("#message");
message.text("Added Content Type Successfully");
});

call.fail(function (data, errorcode, errormessage) {
alert("Could not enable content types: " + errormessage);
});
}

AssociateContentType

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: