4

I tried the below to get all item count of a list but it doesn't work

/_api/Web/Lists/getByTitle('<list title>')?$select=ItemCount,Items&$expand=Items

Any ideas how i can get an itemcount as well as retrieving the fields in my select? How can i also retrieve itemCount with filtering?

Thanks in Advance

0

Use REST API to get items you need, then get no.of items using data.d.results.length.

function getItems(url) {    
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        }
    });
}

getItems( "/_api/Web/Lists/GetByTitle('<list title>')/Items" ).done(function(data){

    var numofItems = data.d.results.length;
     data.d.results.forEach(function(item){ 
        console.log( item.ID);
     });
});
6
  • 1
    ItemCount worked for us. Thank you very much! We didn't use GetItems due to the limit on the number of items that can be returned at once and it had a quicker response.
    – Ryan Exner
    Oct 8 '19 at 16:46
  • 1
    [Ryan Exner] is correct. If you have more items than the OData limit, the list that is returned will not be complete. docs here The default limit seems to be 100 items.
    – Tom Wilson
    Jan 23 '20 at 17:34
  • True, there is the possibility of OData limit. /_api/web/lists/getbytitle('<list title>')/ItemCount is the correct way, so @Suresh Bolineni answer better.
    – Vishnu S
    Jan 24 '20 at 4:45
  • ItemCount is tricky as it will return anumber of all items, also those to which user in context doesn't have access to
    – Diomos
    Mar 18 '20 at 10:21
  • Is there a way to pass $filter to this count query? I need to get the total count of a filtered dataset. I cannot use dataset.length because I'm using skip & top (for paging) and needs to get the total count in a separate query. Oct 10 '20 at 17:28
13

You could use the rest API below as mentioned.

/_api/web/lists/getbytitle('<list title>')/ItemCount

You will get item count of any list or library

you have one more option to retrieve both

You could construct the following query to return items and items count:

/_api/Web/Lists/getByTitle('<list title>')?$select=ItemCount,Items&$expand=Items

In success function you could write the below code to get

 var itemsCount = data.d.ItemCount;
    var items = data.d.Items.results;  
5
  • Thanks Suresh but your recommendation doesn't seem to work
    – naijacoder
    Dec 13 '17 at 22:01
  • I have implemented the above query in my solution which is working for me to retrieve the list items and item count. do you want list items of fields ? Dec 14 '17 at 4:55
  • I wanted to find the item count in a list with more than 5000 items, and this code works for me.
    – entryton
    Sep 12 '19 at 16:30
  • Is there a way to pass $filter to this count query? I need to get the total count of a filtered dataset. I cannot use dataset.length because I'm using skip & top (for paging) and needs to get the total count in a separate query. Oct 10 '20 at 17:28
  • No. You cannot apply filter on this Nov 18 '20 at 16:46
5

It is better to use:

"/_api/web/lists/getbytitle('<list title>')/ItemCount"

instead of:

"/_api/Web/Lists/GetByTitle('<list title>')/Items"

because "/ItemCount" is faster with quick response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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