2

How do I get the total number of items in a list based on a filter. For example:

How many fruits in the SharePoint list, "Fruits", are red?

I can get the items themselves like below, but I just want the number.

_api/web/lists/getbytitle('Fruits')/items?$filter=((color eq 'red'))
matt
  • 1,305
  • 5
  • 30
  • 45

1 Answers1

5

According the OData spec there are supposed to be operations like count and inlinecount but, as far as I've seen, they don't work with the SharePoint 2013 REST API.

Your best bet is to run the query you have above and then check the length property of the array that's returned.

Update: I just though of another option, using the SharePoint 2010 REST API. Your query would look like

_vti_bin/ListData.svc/Fruits/$count?$filter=((color eq 'red'))
Rob Windsor
  • 12,648
  • 25
  • 39
  • Thanks, if anyone is wondering the documentation is here: https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#bk_supported – matt Feb 17 '15 at 19:31
  • The SP 2010 solution proposed by @Rob is returning errors : "Unsupported media type requested" – Aamir Jun 24 '16 at 18:27
  • 1
    I just tried it on my 2013 on-premises farm and my SharePoint Online tenant. It worked in both cases. – Rob Windsor Jun 24 '16 at 19:30
  • 2
    The response is just a number in plain text. Maybe that's the issue you're having. – Rob Windsor Jun 24 '16 at 19:33
  • @Rob, you are right! That was the problem indeed. Now I am trying to figure out how to retrieve that number in SP designer WF. What path should I be specifying. d/results and value won't work. – Aamir Jul 19 '16 at 05:55