5

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

4 Answers 4

14

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
    Commented Dec 13, 2017 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 ? Commented Dec 14, 2017 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
    Commented Sep 12, 2019 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. Commented Oct 10, 2020 at 17:28
  • No. You cannot apply filter on this Commented Nov 18, 2020 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.

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
    Commented Oct 8, 2019 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
    Commented Jan 23, 2020 at 17:34
  • 1
    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
    Commented Jan 24, 2020 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
    Commented Mar 18, 2020 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. Commented Oct 10, 2020 at 17:28
0

August 2023:

I am using 0365 SharePoint and this did NOT work for me:

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

but this did:

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

Note the removal of web/ This makes a very fast call and doesn't abuse bandwidth by pulling rows (even select=Id) for no reason and bypasses the REST/oData limits.

Here is what I use. If you do not want to use odata=verbose in the headers you will want to return data.value vs data.d.ItemCount.

/**
 Gets list item count avoids oData limits
 @date      08/21/2023
 @param     {string}    listname  Name of the list.
 @param     {callback}  success   The success callback
 @param     {callback}  failure   The failure callback
 */
getListItemCount(listname, success, failure) {
    var url = common.myWebUrl() + "/_api/lists/getbytitle('" + listname + "')/ItemCount";

    $.ajax({
        url: url,
        async: this.useAsync,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.ItemCount); 
        },
        error: function (data) {
            failure(data);
        }
    });

}

hth

Also, I use a common file for my webUrls based on the project. It's always something like the this:

    const webUrl = window.myContext["webAbsoluteUrl"]; //window.webPartContext.pageContext.web.absoluteUrl;
    return webUrl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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