0

I've noticed that SharePoint has a limitation of 100 items when using REST calls to a list. I'm trying to get it to work so it make batch calls (i.e if the first call is 133 items, make two calls so the first recieves the first 100 and the second the last 33).

This is my (edited) code:

this.getListItem = function ($scope, listName, url, SPHostUrl, SPAppWebUrl) {
    return getListItem(listName, url, SPHostUrl, SPAppWebUrl).done(function(data) {
        return data;
    });
};

And my function for retrieving the data from SharePoint.

function getListItem(listName, url, SPHostUrl, SPAppWebUrl) {

var deferred = $.Deferred();
var resultsArray = [];
var scriptbase = SPHostUrl + "/_layouts/15/";

jQuery.getScript(scriptbase + "SP.RequestExecutor.js", getOrderDetails);

  function getOrderDetails() {
    var executor = new SP.RequestExecutor(SPAppWebUrl);
    executor.executeAsync(
        {
            url: url,
            method: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            },
            success: function (data) {
                var response = JSON.parse(data.body);
                resultsArray.push(response.d.results);

                if (response.d.__next) {
                    url = response.d.__next;
                    getOrderDetails(); <- Runs multiple times if __next is true
                }

                deferred.resolve(resultsArray)
            },
            error: function (data, errorCode, errorMessage) {
                alert(errorMessage);
            }
        }
    );
  }
return deferred.promise();

}

This works (it gets all the list items in two calls, first gives 100 and second the last 33), but doesn't return the data as i expected. Am I using the promise wrong? Or is .done on the service call ruining it?

EDIT: The code was working fine. The one thing was I was not handling the resultsArray.push() correctly. Needed to loop the results array instead of pushing it directly (only returned two indexes, because of the two calls. Now it returns 133):

$.each(response.d.results, function (index, item) {
    resultsArray.push(item);
});
Robin
  • 740
  • 3
  • 12
  • 30

2 Answers2

0

Your code looks ok to me. I can only say i use myself always

functionCall(param)
            .then(
            function () {
               console.log('success');
            },
            function (sender, args) {
                console.log('fail');
            });

Maybe that's worth a try?

This might also be an interesting topic: jQuery deferreds and promises - .then() vs .done()

Community
  • 1
  • 1
Verthosa
  • 1,671
  • 1
  • 15
  • 37
0

The code was working fine. The one thing was I was not handling the resultsArray.push() correctly. Needed to loop the results array instead of pushing it directly (only returned two indexes, because of the two calls. Now it returns 133):

$.each(response.d.results, function (index, item) {
    resultsArray.push(item);
});
Robin
  • 740
  • 3
  • 12
  • 30