0

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 ?

2 Answers 2

1

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.

2
  • It looks like my ViewQuery for the Task list is malformed => ViewQuery: "'CompletedNot Applicable/>CompletedNot Applicable>" Jan 11, 2021 at 17:29
  • Is it coming like this? Either it is malformed or not complete query. Can you add the response of <siteUrl>/_api/web/Lists/getbytitle('Tasks')/views/getbytitle('Active Tasks')/ViewQuery to your question? Jan 12, 2021 at 3:05
1

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

1

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.