0

Quick question for you here. So I'm trying to do a REST calculation on how many list items there are but the list only returns up to 1,000 entries at a time. I think I got a work around but how would I go about adding the two results?

        $.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$inlinecount=allpages", function (data) {
        $("#ALLCount1").text(data.d.results.length);
    })

    $.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$inlinecount=allpages&$skiptoken=1016", function (data) {
        $("#ALLCount2").text(data.d.results.length);
    })
Xeverus
  • 125
  • 13

2 Answers2

2

Total Item Count

If you are looking for the total number of items in the list itself (as opposed to the number of items in a filtered view) you can directly access the list's $count property.

"/_vti_bin/ListData.svc/MPIC/$count"

This will return the item count.

Paging (if necessary)

To simulate paging using REST you can do the following:

  1. Use the $skip=n parameter to skip the first n entries according to the $orderby parameter
  2. Use the $top=n parameter to return the top n entries according to the $orderby and $skip parameters.

Dealing with Asynchronous Callbacks

The biggest problem in your code is that you've got two asynchronous function callbacks that you use to get the sum of resulting items. Since those function calls are not executed within the same scope, you can't access their values simultaneously.

A workaround is to use function chaining to move your logic forward into the callback function(s).

Example using $skip and $top

Here's an example using the $skip and $top parameters with your provided code, using a recursive callback function to get the total number of results before updating the text of your ALLCount1 element.

$.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$orderby=Id&$top=1000&$inlinecount=allpages", function (data) {
    var count = data.d.results.length;
    updateTotal(count);
});

function updateTotal(total){
    $.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$orderby=Id&$skip="+total+"&$top=1000&$inlinecount=allpages", function (data) {
        var count = data.d.results.length;
        if(count > 0){
            updateTotal(total+count);
        }
        else{
            $("#ALLCount1").text(total);
        }
    });
}

Example using $skiptoken and $top

If you're constrained to use $skiptoken instead of $skip (such as when querying a list of 5000+ items), you can definitely still use it, but you'll have a bit more work to do.

Whereas $skip just wants the number of items to skip, $skiptoken wants the ID of the last item to skip.

The code would be mostly the same as above, but you'll need to dive into the data results and pluck out the last item's ID value. I'll leave that as an exercise for the reader.

updateTotal(0,0);
function updateTotal(skip,total){
    $.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$orderby=Id&$skiptoken="+skip+"&$top=1000&$inlinecount=allpages", function (data) {
        var count = data.d.results.length;
        if(count > 0){
            var lastItemId;
            // TODO: get last item id from results here
            updateTotal(lastItemId,total+count);
        }
        else{
            $("#ALLCount1").text(total);
        }
    });
}
Thriggle
  • 7,009
  • 2
  • 26
  • 37
0

To get the sum you might try something like this:

var sum = 0;

$.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$inlinecount=allpages", function (data) {
    sum = data.d.results.length;
    $("#ALLCount1").text(sum);
    $.getJSON("/_vti_bin/ListData.svc/MPIC?$select=Id&$inlinecount=allpages&$skiptoken=1016", function (data) {
        sum += data.d.results.length;
        $("#ALLCount2").text(sum);
    })  
})

Although this has some problems:

  1. For one, why are you skipping to 1016? Wouldn't you want 1001?
  2. You only have two calls here. How do you plan on retrieving record 2001?

What you probably want to do is paging. That is, keep sending requests until no records are returned.

Matt
  • 4,462
  • 5
  • 25
  • 35
  • So when I look at the first count. It has a next that starts at 1016. I think because the first 16 documents were deleted so it. How would paging work? I've found some comments mentioning it but nothing specific on how to execute. – Xeverus Jun 17 '16 at 18:33
  • You still want to limit it to 1000, unless you know how many are deleted all of the time. It is better to limit it to 1000, have it return 984, and get an accurate count. – Matt Jun 17 '16 at 19:00
  • [this example](http://stackoverflow.com/questions/6951191/do-while-javascript-issue) should get you started. It has some example code that will require modification, but you can post another question if you have trouble with it. – Matt Jun 17 '16 at 20:35