Total item count in SPO Rest API filter query for a document library

Anantha Rengan Kannan 0 Reputation points
2025-02-14T01:41:54.51+00:00

Hi

We are using SPO Rest API query like below for filtering documents in a document library

https://example.sharepoint.com/_api/web/lists/getbytitle('DocLibrary1')/items?$select=*,FileLeafRef,FileRef,Author/Title,Editor/Title,File/Length&$expand=Author,Editor,File&$top=50&$filter=FSObjType eq 0 and substringof('File1', FileLeafRef)&$count=true&$inlinecount=allpages&$orderby=Modified desc

We are using pagination and returning top50 items and we will use NextPageURL to get further data in subsequent calls. The query is getting us the results as expected

But we have a requirement now to get total item count for the above search query, for example if the rest api query with this filter has 1500 items it should return that count as part of the result set of this query.

We already tried '&$inlinecount=allpages' and '&$count=true' but it is not giving us the total item count for the rest api filter query. Is there any way to achieve this requirement.

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sagar Shah 0 Reputation points
    2025-02-14T01:54:35.51+00:00

    The main issue could be related to using $top and $orderby with $count=true. When you apply pagination ($top and $skip), SharePoint REST API might not always provide the total count as expected.

    Here is a slightly adjusted version of your query that might help:

    https://example.sharepoint.com/_api/web/lists/getbytitle('DocLibrary1')/items?$select=FileLeafRef&$filter=FSObjType eq 0 and substringof('File1', FileLeafRef)&$count=true

    In this case, we’re simplifying the query by removing $top, $orderby, and $expand. The result will give you the total count of all the items that match the filter, and then you can use pagination with another query for the actual item data:

    https://example.sharepoint.com/_api/web/lists/getbytitle('DocLibrary1')/items?$select=*,FileLeafRef,FileRef,Author/Title,Editor/Title,File/Length&$expand=Author,Editor,File&$top=50&$filter=FSObjType eq 0 and substringof('File1', FileLeafRef)&$orderby=Modified desc


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.