How to get listitem counts via rest api
-
07-01-2021 - |
Question
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
Solution
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);
});
});
OTHER TIPS
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;
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange