65.9K
CodeProject is changing. Read more.
Home

Sharepoint 2013 REST API, How to get ContentType?

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jul 10, 2015

CPOL

1 min read

viewsIcon

16595

A trick to get contenttype of items, if you unable to fetch it with general way from sharepoint 2013 list.

Introduction

Sharepoint 2013 users widely use REST API's to get data from sharepoint on client side. A commonly used API is "/_api/web/lists/getbytitle('MyList')/Items".  I am providing trick related to this API.

Background

I faced problem in gatting ContentType of item from list using "_api/web/lists/getbytitle" function. I used CamleQuery builder but code provided by CamleQuery builder did not work during actual implimentation.

Using the code

A REST API url which i used before is as follows :

Blocks of code should be set as style "Formatted" like this:

// Before updating
// 

var url = _spPageContextInfo.webAbsoluteUrl;

url += "/_api/web/lists/getbytitle('Engagement Videos')/Items?$orderby=VideoLikeCount desc";

url += "&$select=Id,Title,FileLeafRef,FileRef,AuthorId,OData__SourceUrl,VideoSetDescription,VideoDescription,VideoSetExternalLink,Created,Created_x0020_Date,AlternateThumbnailUrl,VideoBusiness,VideoFunction,EncodedAbsWebImgUrl,EncodedAbsThumbnailUrl,FileDirRef,LinkFilename,LikesCount,VideoSetDefaultEncoding,Author/Id,Author/Title,TargetBusiness,VideoLikeCount,ContentType";

url += "&$expand=Author/Id,Author/Title";
url += "&$top=5";

If you observe above url, it has "ContentType" columon. This is a code generated by CamleQuery builder, but suprizingly it did not worked. This code wont give you error but it simply skip "ContentType" columon from result.

Now here is my trick to get it done. Modify above url as below :

// Code after updating 

var url = _spPageContextInfo.webAbsoluteUrl;
url += "/_api/web/lists/getbytitle('Engagement Videos')/Items?$orderby=VideoLikeCount desc";

url += "&$select=Id,Title,FileLeafRef,FileRef,AuthorId,OData__SourceUrl,VideoSetDescription,VideoDescription,VideoSetExternalLink,Created,Created_x0020_Date,AlternateThumbnailUrl,VideoBusiness,VideoFunction,EncodedAbsWebImgUrl,EncodedAbsThumbnailUrl,FileDirRef,LinkFilename,LikesCount,VideoSetDefaultEncoding,Author/Id,Author/Title,TargetBusiness,VideoLikeCount,ContentType,ContentTypeId,ContentType/Id,ContentType/Name";

url += "&$expand=Author/Id,Author/Title,ContentType";
url += "&$top=5";

If you observe above url, i have added "ContentTypeId,ContentType/Id,ContentType/Name" columons in $select and also  ContentType in $expand. This will give you correct expected result.

Hope this will help and I am happy to help.

Points of Interest

1] "ContentType" in $expand

2] "ContentTypeId,ContentType/Id,ContentType/Name" in $select

3] My list was of type Asset Library and Like functinality was enable on it.

4] Also Approval workflow was running on list.

Hope this will help and I am happy to help.    :)