9

I can't for the life of me figure out what's wrong with this URL:

_spPageContextInfo.webAbsoluteUrl + String.format("/_api/web/lists/getbytitle('{0}')/items?$select=ContentType/Name&$expand=ContentType&$filter=ContentType/Name eq 'Highlight Report'", documentsListTitle)

I get

400 (Bad Request)

Note I'm specifically looking for a REST solution with an example if possible.

3
  • getbytitle('{0}') ? I thought there should be the listname...
    – Patrick
    Nov 25, 2015 at 15:46
  • Notice the use of String.format.
    – Submits
    Nov 25, 2015 at 15:47
  • To the close voters: The question is regarding REST API not server side or client side using C#. Hope you understand. Nov 26, 2015 at 9:58

3 Answers 3

15

Found a way which works for me. SharePoint 2013 on-prem.

Starting point:

A document library with two content types Document & Highlight Report and two documents in it for testing purpose.

enter image description here

REST URL:

The REST URL for filtering only the document with the content type Document is:

https://<yourdomain>/_api/lists/getbytitle('<yourlistname>')/items?$filter=ContentType eq 'Document'

The output is:

enter image description here

If you want to get the document lorem1 which is content type Highlight Report the REST URL looks like this:

https://<yourdomain>/_api/lists/getbytitle('<yourlistname>')/items?$filter=ContentType eq 'Highlight Report'

enter image description here

Hope this could help you.

15
  • Just tried this and I get back a zero length array, which probably means no items were returned, which means the filter isn't working (I should get two items).
    – Submits
    Nov 26, 2015 at 9:23
  • What is the name of the library and of the content type? Could you try to get first all your items with /_api/web/lists/getbytitle('LIBRARYNAME')/items
    – Patrick
    Nov 26, 2015 at 9:27
  • Yes using that URL /_api/web/lists/getbytitle('Documents')/items I get back all the items from the list. But when I add ?$filter=startswith(ContentType,'Highlight Report') I get back nothing.
    – Submits
    Nov 26, 2015 at 9:46
  • 1
    Filtering on content type name also works for me without the use of CAML. Check my updated answer.
    – HarryB
    Nov 26, 2015 at 10:52
  • 1
    Updated the answer with english pictures and the content type you are using.
    – Patrick
    Nov 26, 2015 at 11:05
4

$filter by ContentType is not supported. Reason could be ContentType is not any column of list OR Microsoft does not support it yet. $expand is supported.

Ignore the &$filter=ContentType/Name eq 'Highlight Report'" and see it works.

Update #1:

Following solution is regarding CAML + C#

Filter By ContentType using CAML

Now make it CAML + REST API

<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name='ContentType'/>
        <Value Type='Computed'>Highlight Report</Value>
      </Eq>
    </Where>
  </Query>
</View>

For executing CAML in REST API

End-Point:

String.format("/_api/web/lists/getbytitle('{0}')/getitems", documentsListTitle)

HTTP Method:

POST

Request Body:

var query = "<View>" + 
 "<Query>" + 
 "<Where>" + 
 "<Eq>" + 
 "<FieldRef Name='ContentType'/>" + 
 "<Value Type='Computed'>Highlight Report</Value>" + 
 "</Eq>" + 
 "</Where>" + 
 "</Query>" + 
 "</View>";

var data = {
    query: {
        __metadata: {
            'type': 'SP.CamlQuery'
        },
        ViewXml: query
    }
};

Let me know for more modification or explanation.

5
  • Yep, it works without the filter but then how do I filter by the name of a content type? Surely there's a way
    – Submits
    Nov 25, 2015 at 15:59
  • did not find any using REST yet. there could way using CSOM. If you are dealing with very few items, then filter them in request complete like success function Nov 25, 2015 at 16:04
  • See my last update @Submits Nov 26, 2015 at 5:53
  • ok ...... waiting to hear from you Nov 26, 2015 at 9:22
  • Brilliant, it worked. It's a shame you have to use CAML to achieve this though! Thanks.
    – Submits
    Nov 26, 2015 at 10:14
2

Try using ContentTypeId as the filter , the example below is returning results for me:

http://site/_api/web/lists/getbytitle('Reusable%20Content')/items?$select=ContentType/Name&$expand=ContentType&$filter=ContentTypeId%20eq%20%270x01002CF74A4DAE39480396EEA7A4BA2BE5FB009CBCDDE56A142247B896B1DE93CE3843%27

Update 1: Filtering on ContentType field also works. Try by replacing the space within the content type name by %20 in your REST Url

http://site/_api/web/lists/getbytitle('Reusable%20Content')/items?$select=ContentType/Name&$expand=ContentType&$filter=ContentType%20eq%20%27Reusable%20HTML%27
3
  • ContentTypeId is not an option because this site will become a site template and I'm not sure the content type IDs will remain the same.
    – Submits
    Nov 25, 2015 at 17:49
  • You can get the content type Id dynamically by making a request like this: /_api/web/ContentTypes?$select=Name,Id&$filter=Name%20eq%20%27Item%27
    – HarryB
    Nov 25, 2015 at 17:56
  • You're right, this could be a solution. The only down side is I would need to make two REST calls.
    – Submits
    Nov 26, 2015 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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