1

I am trying to retrieve list item data from different site collection in SP2013.

I am storing all site URL in an array and I am iterating this using for loop. while in for loop I have called another method which has REST code and I am passing site URL one by one to REST.

However this is not working as for loop keeps incrementing and REST is executing for only 1 site URL and giving me data from first site only. I think this is happening because of Async call.

SiteURL[] has 3 url and I want to get data from this 3 sites.

Code sample:

function getSubmissionNo(siteURL)
{        
   var currentNewUrl;
   for(a=0;a<=siteURL.length;a++)
   {            
      currentNewUrl=siteURL[a];  
      getDataUsingRest(currentNewUrl).done(function(result){})
   }    
}
function getDataUsingRest(requiredUrl)
{
   var dfd=new $.Deferred;
   $.ajax({
        url: requiredUrl+"/_api/web/lists/getbytitle('E-files')/items?$select=ImgURL,FileRef&$expand=ContentType&$filter=ContentType eq 'Submission Folder'",
        type: "GET",  
        asyn: false,            
        headers: {
            "Accept": "application/json;odata=verbose",
        },

        success:function(data)
        {
            dfd.resolve();
            dataReturned(data);
        },
        error:function(data)
        {
            dfd.reject();

        }

    });
  return dfd.promise();
}
function dataReturned(data)
{   
  var k;
  for(k=0;k< data.d.results.length;k++)
  {
     var item=data.d.results[k];
     var subN = item.ImgURL;
     var fileRefTest = item.FileRef;
     console.log(fileRefTest);
  }
}
2

I have modified you code to replace for loop with recursive call. Check below code try from you side.

var allSiteResults = [];
function getSubmissionNo(siteURL, siteCount) {
    if (siteCount < siteURL.length) {
        siteCount++;
        var currentNewUrl;
        for (a = 0; a <= siteURL.length; a++) {
            currentNewUrl = siteURL[a];
            getDataUsingRest(currentNewUrl).then(function (data) {
                allSiteResults.push(data);
                getSubmissionNo(siteURL, siteCount);
            }).fail(function (error) {
                console.log(error);
            })
        }
    }
    else {
        dataReturned(allSiteResults);
    }
}
function getDataUsingRest(requiredUrl) {
    var dfd = new $.Deferred;
    $.ajax({
        url: requiredUrl + "/_api/web/lists/getbytitle('E-files')/items?    $select=ImgURL,FileRef&$expand=ContentType&$filter=ContentType eq 'Submission Folder'",
        type: "GET",
        asyn: false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            dfd.resolve(data);
        },
        error: function (data) {
            dfd.reject(data);
        }
    });
    return dfd.promise();
}
function dataReturned(allSiteResults) {
    $.each(allSiteResults, function (index, data) {
        var k;
        for (k = 0; k < data.d.results.length; k++) {
            var item = data.d.results[k];
            var subN = item.ImgURL;
            var fileRefTest = item.FileRef;
            console.log(fileRefTest);
        }
    })
}
getSubmissionNo(siteURL, 0);
  • Thank you so much @Bharat Khunti:). I tried your code and it is working perfectly. I was struggling for this issue from last 3 days. I have marked this as answer – Vishal_chorghe Aug 7 '18 at 8:09

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.