Click here to Skip to main content
15,893,487 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2013
Tip/Trick

Sharepoint 2013 REST API, How to get ContentType?

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Jul 2015CPOL1 min read 16.2K  
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 :

C++
<span style="font-size: 9pt;">// Code after updating </span>
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.    :)

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --