4

On an onchange event on a select box (filtered lookup column where the value is the item ID) i make an AJAX call to auto fill certain fields on a form.

This works great except for it only returns 100 items (currently the list is pushing 200 items) and as such often a person will select an item for it then to do nothing. I am new to AJAX/REST and can not tell what I need to change.

Code:

  var requestUri = _spPageContextInfo.webAbsoluteUrl +
                         "/_api/Web/Lists/getByTitle('Traveller Details')/items";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            //loop
            var i = 0;

            $.each(data.d.results, function (index) {
                var e = document.getElementById("ctl00_ctl45_g_c16cce21_47f1_40fc_ab50_f38e7df1b86c_ff931_ctl00_Lookup");
                var strUser = e.options[e.selectedIndex].value;

                i++; //Counter i used to find out that it is exactly 100

                //If account found
                if ($(this).attr('ID') == strUser) {
                   //Sets field attributes here.
10

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);
}
  • Thanks this does explain a lot. I am not implementing this in this specific case as I found a way to filter by ID (since I am only interested in getting 1 item with a known ID). Code: $filter=(ID eq 215) – Roland Feb 11 '16 at 4:28
  • You can also set top to some number greater than 100 to retrieve more items in the initial call. I think that values can go as high as 1000, but I'm not positive. – Marc D Anderson Feb 15 '16 at 3:53
  • 1
    @Roland If you're looking for a specific item, then /Items(id) should be your endpoint. No need to filter for it. – wjervis Feb 15 '16 at 17:20

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.