2016-06-07 75 views
0

我注意到,在將REST調用使用到列表中時,SharePoint有100個項目的限制。我試圖讓它工作,所以它進行批次調用(即如果第一個電話是133項,做兩個電話,所以第一個接收前100和第二個最後33)。從SharePoint(加載項)跨域獲取超過100個承諾的列表項

這是我(編輯)代碼:

this.getListItem = function ($scope, listName, url, SPHostUrl, SPAppWebUrl) { 
    return getListItem(listName, url, SPHostUrl, SPAppWebUrl).done(function(data) { 
     return data; 
    }); 
}; 

而且我對從SharePoint檢索數據的功能。

function getListItem(listName, url, SPHostUrl, SPAppWebUrl) { 

var deferred = $.Deferred(); 
var resultsArray = []; 
var scriptbase = SPHostUrl + "/_layouts/15/"; 

jQuery.getScript(scriptbase + "SP.RequestExecutor.js", getOrderDetails); 

    function getOrderDetails() { 
    var executor = new SP.RequestExecutor(SPAppWebUrl); 
    executor.executeAsync(
     { 
      url: url, 
      method: "GET", 
      dataType: "json", 
      headers: { 
       Accept: "application/json;odata=verbose" 
      }, 
      success: function (data) { 
       var response = JSON.parse(data.body); 
       resultsArray.push(response.d.results); 

       if (response.d.__next) { 
        url = response.d.__next; 
        getOrderDetails(); <- Runs multiple times if __next is true 
       } 

       deferred.resolve(resultsArray) 
      }, 
      error: function (data, errorCode, errorMessage) { 
       alert(errorMessage); 
      } 
     } 
    ); 
    } 
return deferred.promise(); 

} 

此作品(它得到兩個調用的所有列表項,首先對100和第二最後的33),但如我所料不返回數據。我用諾言錯了嗎?或者是在服務調用中破壞它?

編輯:代碼工作正常。有一件事是我沒有正確處理resultsArray.push()。需要循環的結果數組,而不是直接將其推(只返回了兩個指標,因爲在這兩個電話的現在,它會返回133):

$.each(response.d.results, function (index, item) { 
    resultsArray.push(item); 
}); 

回答

0

的代碼是工作的罰款。有一件事是我沒有正確處理resultsArray.push()。需要循環結果數組而不是直接推送(由於兩次調用,只返回兩個索引,現在它返回133):

$.each(response.d.results, function (index, item) { 
    resultsArray.push(item); 
}); 
0

您的代碼看起來不錯給我。我只能說我總是用我自己

functionCall(param) 
      .then(
      function() { 
       console.log('success'); 
      }, 
      function (sender, args) { 
       console.log('fail'); 
      }); 

也許這是值得一試?

這也可能是一個有趣的話題:jQuery deferreds and promises - .then() vs .done()

+0

感謝您指出了這一點。查看我的編輯解決方案。 – Robin

相關問題