0

I need to call multiple rest calls for getting the information from the SharePoint Online. Iam using java script.

First I need to get all subsites and then I need to get all lists information from each subsite. I managed to get the information using REST calls. I need to know if there is a best practice for calling multiple REST calls.

I need to ensure that after successful completion of one API call, the subsites call, I can then make the lists call.

Can anyone suggest best approach with sample code if possible? I need to call multiple get calls one by one after successful completion previous call. Currently i am storing all the subsite urls in an array and doing for each on the array to store list urls in another array.

3

Couple of comments

  • success CallBacks are 2016, even jQuerys $.ajax has supported .done and .fail Promises for ages

  • using async=false solves your async problem but now your whole page and the rest of the script are waiting for those (rather slow) SP API calls
    Learn to work with Promises (requires a Polyfill for IE, because it doesn't support the Web Standard yet)
    or use jQuerys non-standard implementation of Promises

  • Nowadays you don't want to stick jQuery.ajax all over the place, Microsofts new SharePoint Framework uses its own HttpClient
    So you want to call jQuery only from ONE function, so you can easily refactor

  • jQuerys $.each function can be replaced with standard JavaScript.. faster.. shorter

  • The Web endpoint already contains information about its Lists
    But they are __deferred,
    So you can use that uri (when you go the multiple calls & Promises route)

OR.. it is all standard OData...
tell the Server to include it with ?$expand=Lists

Code will then be something like:

I am not using jQuery myself but HTML5 Promises,
so you have to add the .when code yourself

console.clear();
function ajaxCall(endpoint, doneFunction) {
    $.ajax({
        url: endpoint,

        //ONLY USE  async=false FOR A QUICK FIX
        //WHEN YOU DON'T CARE EVERYTHING HALTS TILL THE REST CALL IS FINISHED
        //WHEN YOU DON'T CARE ABOUT USING PROMISES
        //WHEN YOU ARE *NOT* A PROGRAMMER WHO WANTS TO DO IT RIGHT
        async:false,  

        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose"
        }
    })
        .done(doneFunction)
        .fail(function(err) {
                console.error(err);
            }
        );
}
var siteLists = {};
ajaxCall(
    _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Webs?$expand=Lists",
    function (data) {
        data.d.results.forEach(function (web, webidx) {
            siteLists[web.Title] = web;
            console.log(webidx,web.Title,web.Lists);
        })
    });

console.info(siteLists);
  • If you want the ListItems you make it:

     _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Webs?$expand=Lists,Lists/Items",
    

You will sure notice a wait then

iREST

2

Here is the code you are looking for..

I have tried to fetch all the subsites in one call and then made call to each subsite to fetch all list items..

so number of calls is 1 + No. of subsites in your site collection

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/webs/?$select=title,ServerRelativeUrl",
    type: "GET",        
    async:false,        
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        $.each(data.d.results,function(ind,val){
            console.log("Site : " + val.Title);
            console.log('Lists');
            getLists(val.ServerRelativeUrl);
        })
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
function getLists(ServerRelativeUrl){
    $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl +"/" + ServerRelativeUrl +"/_api/Web/Lists",
    type: "GET",
    async:false,        
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        $.each(data.d.results,function(ind,val){
        //  debugger;
            console.log("        "+ val.Title);
        })
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});

}
  • Thanks for the reply. Will try the above code and update you – TARUN Feb 3 '17 at 12:01
0

I assume you are working with javascript.

You can achieve this by putting the code for next rest call in the success handler of the previous rest call. You should store the current index from the array in some global variable. So, you would need the function that calls the rest api for the current url from array, and when it finishes, the success handler function should make the lists rest call for that subsite. The lists rest call function should have the success handler that increases the current array index and calls the first function again. The calls should finish when you get to the end of your array.

More about rest calls, and examples of succes handler functions can be found here.

  • Hi Damjan Tomic thanks for the reply. Iam using java script. Can you please provide any sample code please – TARUN Feb 3 '17 at 2:46

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.