Monday, October 9, 2017

AJAX/Rest only returning 100 items instead of full list

The OData provider for the REST API is configured to limit the number of items in a response to 100. It protects against developers inadvertently making a request that returns large result sets.
If there are more items the response will contain a continuation. In a JSON response the continuation will be a property named __next.
You can use $top if you want to override this behavior and have a large result set to be returned.
var url = appUrl + "/_api/Web/Lists/getByTitle('Order Details')/Items";  

var message = jQuery("#message");
message.text("");
getItems();  

function getItems() {
    var call = jQuery.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        message.append(String.format("Retrieved {0} items",
            data.d.results.length));
        message.append("<br/>");

        if (data.d.__next) {
            url = data.d.__next;
            getItems();
        }
    });
    call.fail(failHandler);
}
or you can always use $top=2000 in query