1

I have created multiple lists in SharePoint 2013. I need to get the multiple list items using REST API call.

I have tried the method to get the multiple list items using API within the for loop but it works slowly.

Please suggest best idea to get multiple lists response.

3
  • do they have same content type ? May 12, 2017 at 4:53
  • 1
    Instead of the loop, you can try by creating an array of your all lists name and post your request with this AJAX call, and in server side, you can retrieve data from all the lists and make a response in JSON.as mentioned here:stackoverflow.com/questions/24744702/… May 12, 2017 at 4:53
  • I don't have any content types . i am getting questions from SP survey list.(or suggest how to get the survey questions and responses using server side code). @GautamSheth
    – PGP
    May 12, 2017 at 5:00

1 Answer 1

3

To get data from multiple list at a same time, we need to parallel Ajax get request for getting data from more than one list.

Let's take one example:

We have two list named "List1" and "List2" and we need to get all items from both lists.

We create one module for get item:

var getModule = (function () {
    var baseRequest = {
        url: "",
        type: ""
    };

    var getApp = {
    };
    var getApp = {
        getRequest: getRequest,
    };

    function getRequest(endpoint, _async) {
        //var dfd = $.Deferred();
        var request = baseRequest;
        request.type = "GET";
        request.url = endpoint;
        request.headers = {
            ACCEPT: "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()

        };
        return $.ajax(request);
        //return dfd.promise();
    };

    return getApp;
})();

After that we used jQuery $.when function for getting data from multiple list as shown below

var list1FilesInfo = $.Deferred();
var list2FilesInfo = $.Deferred();

        var dfdList1Files;
        var dfdList2Files;

        var list1URL = "Your Web URL" + "/_api/Web/lists/getbytitle('List1')/items";
        var list2URL = "Your Web URL" + "/_api/Web/lists/getbytitle('List2')/items"



        dfdList1Files = getModule.getRequest(list1URL, true);
        dfdList2Files = getModule.getRequest(list2URL, true);

        dfdList1Files.done(function (data) {
            list1FilesInfo.resolve(data);
        });
        dfdList2Files.done(function (ldata) {
            list2FilesInfo.resolve(ldata);
        });

        $.when(list1FilesInfo, list2FilesInfo).then(function (list1Data, list2Data) {
            conssole.log('success');
        });

Hope it works for you.

2
  • What's mean by EasyNavModule .and its not declared.
    – PGP
    May 12, 2017 at 5:07
  • Sorry it's typo mistake. please see updated answer May 12, 2017 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.