Sharepoint - Getting an ItemCount with filtering in SP2013 using REST api
The ItemCount
function could be applied to List
resource, for example:
/_api/web/lists/getbytitle('<list title>')/ItemCount
but not to ListItemCollection
resource
You could consider the following options of getting items count for list items:
Option 1
How to return items count from json result:
var endpointUrl = _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Tasks')/items";
getJson(endpointUrl)
.done(function(data)
{
var itemsCount = data.d.results.length; //get items count
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
where
function getJson(url)
{
return $.ajax({
url: url,
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}
Option 2
You could construct the following query to return items and items count:
/_api/Web/Lists/getByTitle('<list title>')?$select=ItemCount,Items&$expand=Items
Example:
var endpointUrl = _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Tasks')?$select=ItemCount,Items&$expand=Items";
getJson(endpointUrl)
.done(function(data)
{
var itemsCount = data.d.ItemCount;
var items = data.d.Items.results;
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
When you query /_api/web/Lists/getbytitle(\'Items\')
basically it returns SPList
object. That object as ItemCount
property.
The value of the ItemCount
property includes folders within a list/document library, as well as items/files within subfolders.
For filtering we need to apply filter on Items
so query /_api/web/Lists/getbytitle(\'Items\')/items?$filter=SubjectsID eq 3&$select=Id
will apply filter and return items.
If you want to use Filtering with Count, then use List services
http://weburl/_vti_bin/listdata.svc/Items/$count?&$filter=SubjectsID eq 3