7

I have a problem with getting the ContentType name for a item.

when i use

http://sp2013dev/sites/mms/_api/web/lists/GetByTitle('List')/items(1)/?$expand=ContentType

I get as response all Fields from the ContentType, which is so far not bad, but if I use also select in the query the ContentType Fields are not included in the results it only returns

Result:

 ContentType: {
__deferred: {
uri: "http://site/_api/Web/Lists(guid'b911f75b-1988-4f44-bd88-39ab510562a4')/Items(1)/ContentType"
}-
}

Query:

http://site/_api/web/lists/GetByTitle('List')/items(1)/?$select=Id,Title,ContentType&$expand=ContentType

And the second problem is that when I use ?$expand=ContentType/Name (I only need the name of the ContentType) it is ignored completely and I get all Fields from the ContentType

Edit:

After trying

/_api/web/lists/GetByTitle('')/items()/?$select=Id,FileRef,ContentType/Id,ContentType/Name&$expand=ContentType

as recommend by Vadim i get the following response

{
  d: {
      __metadata: {
           id: "4b62e4e7-a023-4992-b689-e9cf49f3ef86"
           uri: "<url>/_api/Web/Lists(guid'b911f75b-1988-4f44-bd88-39ab510562a4')/Items(1)"
           etag: ""3""
           type: "SP.Data.AgendaElementsUsedListItem"
     }
     ContentType: {
           __deferred: {
               uri: "<url>/_api/Web/Lists(guid'b911f75b-1988-4f44-bd88-39ab510562a4')/Items(1)/ContentType"
           }
     }
     Id: 1
     ID: 1
     FileRef: "/sites/mms/Lists/AgendaElementsUsed/1_.000"
     }
}
  • How incredibly odd. According to msdn you should be able to use the layout you have (.../Items(1)?$select=ContentType/Description&$expand=ContentType/Description). I did a quick test and this works: $select=*&$expand=ContentType (but doesn't help of course), as well does ...Items(1)/ContentType/Description and .../Items(1)/ContentType?$select=Description. – eirikb Apr 14 '14 at 11:50
  • Yeah these are working at least to get the name of the contentype .../Items(1)/ContentType?$select=Description but then I'am missing some custom fields from the listitem (id,title,...) – Mark Apr 14 '14 at 12:15
  • Yes, I'm starting to think this might be a SharePoint bug actually. ContentType is a bit special, but it should work – eirikb Apr 14 '14 at 12:16
5

Since $expand operator specifies which projected fields from a joined list are returned, the REST query:

/_api/web/lists/GetByTitle('<List Title>')/items(<Item Id>)/?$expand=ContentType

returns list item entity with content type entity.

In order to return the specific item properties along with Content Type properties, you should use $select and $expand operators as demonstrated below:

/_api/web/lists/GetByTitle('<List Title>')/items(<Item Id>)/?$select=Id,FileRef,ContentType/Id,ContentType/Name&$expand=ContentType

enter image description here

  • 1
    This actually works for you? It does not for me, and I have a very strong feeling it does not for OP either – eirikb Apr 14 '14 at 15:20
  • works for me, at least in SharePoint Online – Vadim Gremyachev Apr 14 '14 at 15:21
  • I tried on 15.0.4569.1000. Neither any of the approaches above, or copied directly from your answer, either with direct opening in browser or with application/json;odata=verbose (as I think is what you are using) – eirikb Apr 14 '14 at 15:23
  • 1
    So I did a quick check against SharePoint Online there indeed it is working... So it is a bug in the SharePoint on Premise versions we are using I think ? Could not update to Service Pack 1 because it is currently retracted to have a look if it is fixed there – Mark Apr 15 '14 at 8:08
  • 1
    Ok damn, then I have to wait till SP1 will be out again. I think there will be the fix for this in it. So I will mark this as answer, because I think it will be fixed than in On Premise thanks guys – Mark Apr 15 '14 at 10:51
13

Add ContentTypeId to your select. In an on premise SP 2013 environment I found this is an important key. To return the ContentType Name, it is not enough to expand the content type. You also have to include ContentTypeId in the select. In other words, this will not achieve the desired result:

?$select=Id,ContentType/Name&$expand=ContentType

However, this will:

?$select=Id,ContentTypeId,ContentType/Name&$expand=ContentType
  • This solved my issue. I'm on 2013. – Jordan Oct 9 '15 at 21:48
  • Yes indeed, this does seem to be the trick for SharePoint on premises. Probably should be the accepted answer too. – flayman Apr 25 '18 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.