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

naijacoder
  • 4,272
  • 26
  • 101
  • 188

4 Answers4

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;  
Suresh Bolineni
  • 1,518
  • 12
  • 26
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.

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
Ruslan Korkin
  • 236
  • 3
  • 4
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);
     });
});
Vishnu S
  • 864
  • 1
  • 14
  • 25
  • 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 08 '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
  • 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 Jan 24 '20 at 04: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
  • This answer is wrong if the response is paginated. Use /ItemCount instead. – Pierre Monico Jul 28 '21 at 15:04
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: &quot;GET&quot;,
    headers: { &quot;Accept&quot;: &quot;application/json; odata=verbose&quot; },
    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;
Joe Johnston
  • 287
  • 4
  • 13