7

I am attempting to return an integer (itemcount) of the items within a SP2013 list. I have done this easily using the stock standard json method. However I have a need of counting the splist items which contain a certain value. My silly knowledge immediately lead me to try and use filtering with the ItemCount parameter only to find out that it will always still return the count for all items and it will completely ignore the filter. I have searched for a way of doing this but have found absolutely nothing other than a single page with a comment stating that it is not possible with the REST api and that it can ONLY be done using CSOM.

My question is, firstly, is it true that there is NO way to filter itemcount with REST? If so, how else can I obtain my item count using this type of filtering? (Examples or sources will help vastly) Thanks.

Below is my attempted request:

var itemid = item.id;
          MyService.ensureFormDigest(function (fDigest) {
              $http.get(
                  MyService.appWebUrl + '/_api/web/Lists/getbytitle(\'Items\')/ItemCount?$filter=SubjectsID eq' + itemid,
                  {
                      headers: {
                          'Accept': 'application/json; odata=verbose',
                          'X-RequestDigest': fDigest
                      }
                  }).success(function (d) {
                      item.itemcode = d.d.ItemCount;
                      alert(item.itemcode);
                      callback();
                  }).error(function (er) {
                      alert("Error getting List Item Count: " + er);
                  });
          });
      }

On another note, I was advised to try it as such: /Items?$filter=SubjectsID eq 3&$select=Id

Then in the JS, in my success method:

data.d.results.length
  • <script type="text/javascript"> var siteurl = _spPageContextInfo.webAbsoluteUrl; $.ajax({ url: siteurl + "/_api/web/lists/getbytitle('VolunteerMasterList')/items", method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function (data) { if (data.d.results.length > 0 ) { var sum =0; var sum1 =0; var sum2 =0; var sum3 =0; $.each(data.d.results, function(item){ if(item.City === "Earth") sum++; if(item.VolunteerMasterListFileStatus === "Closed") sum1++; if(item.VolunteerMasterListFileStatus === "Partial") sum2++; if(item.VolunteerMasterListFileStatus === "Special Project – Mani Nov 22 at 19:50
12

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));
});
  • 1
    Thank you for the explanations and options it definitely helped. – geostima Apr 29 '15 at 9:54
5

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

  • Thank you, this is exactly how I implemented it and it works flawlessly. I was curious about other options as suggested by Vadim below and it is good to know that I can also return ItemCount as a parameter. Great Answers, thank you. – geostima Apr 29 '15 at 9:54

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.