Question

I have a Task list that I'm trying to query to get the total Active Tasks for the current user. There is a view already built that I would like to use.

/_api/web/lists/getbytitle('Tasks')/views/getbytitle('My Tasks')/ItemCount

However, it throws an error:

Cannot find resource for the request ItemCount

Is there a way I can grab the item count from an existing view using the REST API ?

Was it helpful?

Solution

There is no direct way to get the items count for list view like you can get for list using /ItemCount endpoint.

Workarounds

Solution 1 (Personally Recommended):

  1. First get the ViewQuery for list view which stores the CAML query for list view. You can get it like:

    <siteUrl>/_api/web/Lists/getbytitle('Tasks')/views/getbytitle('Active Tasks')/ViewQuery
    

    Example Output:

    <Where>
        <Neq>
            <FieldRef Name="Status" />
            <Value Type="Text">Completed</Value>
        </Neq>
    </Where>
    <OrderBy>
        <FieldRef Name="ID" />
    </OrderBy>
    
  2. Using this ViewQuery, you can build CAML query and then fetch list items using getItems() endpoint (POST Method).

  3. Count the number of items returned in 2nd step.

Reference: How to get all items in a view using REST API


Solution 2:

If you want to reduce the number of REST calls then you can use this approach.

  1. First take a note of all filters applied to list view

  2. Get list items using /items endpoint by applying filters noted in 1st step, something like this:

    <siteUrl>/_api/web/Lists/getbytitle('Tasks')/items?$filter=Status ne 'Completed'
    
  3. Count the number of items returned in 2nd step.

Note: If you use this approach then you have to change $filter query every time you change the filter conditions from list view settings.

OTHER TIPS

I don't think you can get a direct item count from a view. You need to retrieve the items from the view and check items.length property to find the count.

You can refer to this post to find out the details

ItemCount property is at the list level You can see this post to find out more

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top