2016-09-26 52 views
0

如何從使用javascript的SharePoint中的不同列表中列出項目。鑑於我所有的列表都存儲在一個數組中。我需要遍歷數組並在每個列表上執行類似的功能。從多個列表中檢索存儲在數組中的列表項目

function initializePage() { 

listcollections.forEach(function (value, index) { // listcollections is the array that contains the list url for different lists 

    var listtitle = value.listTitle; 
    var siteUrl = value.siteURL; 


    getItemsWithCaml(siteUrl, listtitle, 
    function (camlItems) { 


     var listItemEnumerator = camlItems.getEnumerator(); 
     while (listItemEnumerator.moveNext()) { 

      var EventsItem = new Events(); 
      var listItem = listItemEnumerator.get_current(); 

      EventsItem.eventTitle = listItem.get_item('Title'); 
      EventsItem.eventDate = listItem.get_item('EventDate'); 
      EventsItem.eventId = listItem.get_id(); 
      EventsItem.eventSite = siteUrl; 
      EventsItem.eventList = listtitle; 

      EventsCollection.push(EventsItem); 


     } 
    }, 
     function (sender, args) { 
      alert('An error occurred while retrieving list items:' + args.get_message()); 
     });  
}) 

};

function getItemsWithCaml(siteUrl, listtitle, header, success, error) 

{

var hostWebContext = new SP.ClientContext(siteUrl); 

var list = hostWebContext.get_web().get_lists().getByTitle(listtitle); 

var caml = new SP.CamlQuery(); 

//Create the CAML that will return only items expiring today or later 
caml.set_viewXml("<View><Query><Where><Geq><FieldRef Name=\'Expires\'/><Value Type=\'DateTime\'><Today /></Value></Geq></Where> </Query></View>"); 
var camlItems = list.getItems(caml); 
hostWebContext.load(camlItems); 
hostWebContext.executeQueryAsync(
     function() { 

      success(camlItems); 
     }, 
     error 
    ); 

};

//需要執行某些功能格式化每個列表項

//我不能夠取回在一個單一變量的所有列表項,以便能夠從所有列表中顯示的數據一起

回答

0

在下面的例子中,我創建了一個名爲的List對象的ListDataCollection,其中包含一個屬性列表這是一個對象數組。

其中每一個都包含一個Url property whit我想要獲取內容的列表的URL。

然後我循環槽對象數組並調用每個Url的Sharepoint REST api。

每次通話結束:

  • 我與數據當前對象上創建數據屬性由AJAX調用REST API收到 。
  • 我也更新當前對象完成屬性爲true。

然後我叫了一個名爲功能ifAllCompleted是檢查是否所有Ajax調用結束。

當收到所有數據時,我在瀏覽器控制檯上記錄了字已完成

在這一點上,你有一個對象數組。每個對象都包含一個Sharepoint列表的數據。

例如:

ListDataCollection.Lists[0].Data.d.results[0].Title 

將包含在第一列表中的名稱所述第一元件的值。

如果您願意,您可以使用JavaScript中的concat函數合併一個數組中的所有數據。

看看單詞後面的4行代碼已完成

希望這可以幫助!

<script> 
var ListDataCollection = { 
    Lists:[{ 
     Url:"http://Url.Of.Your.Site.Collection/_api/web/lists/getbytitle('List1')/Items", 
     Completed:false 
    },{ 
     Url:"http://Url.Of.Your.Site.Collection/_api/web/lists/getbytitle('List2')/Items", 
     Completed:false 
    },{ 
     Url:"http://Url.Of.Your.Site.Collection/_api/web/lists/getbytitle('List3')/Items", 
     Completed:false 
    }] 
}; 

function ifAllCompleted() { 
    for (var i=0;i<ListDataCollection.Lists.length;i++) { 
     if (!ListDataCollection.Lists[i].Completed) { 
      return false; 
     } 
    } 
    console.log('Completed'); 
    var arrayOfAllData = ListDataCollection.Lists[0].Data.d.results; 
    arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[1].Data.d.results); 
    arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[2].Data.d.results); 
    console.log('Total elements : ' + arrayOfAllData.length); 
} 

$(document).ready(function(){ 
    for (var x=0;x<ListDataCollection.Lists.length;x++) { 
     $.ajax({ 
      url:ListDataCollection.Lists[x].Url, 
      indexValue:x, 
      method: "GET", 
      headers: { "Accept": "application/json; odata=verbose" }, 
      success: function (data, status, xhr) { 
       ListDataCollection.Lists[this.indexValue].Data = data; 
       ListDataCollection.Lists[this.indexValue].Completed = true; 
       ifAllCompleted(); 
      }, 
      error: function (xhr, status, error) { 
       console.log('error'); 
      } 
     });  
    } 
}); 
</script> 
+0

非常感謝!我確實通過爲每個列表創建不同的函數來解決我的問題,但我的代碼看起來很糟糕。我會試試這個。 –

+0

我用你的方法解決了我面臨的另一個問題。完美的作品!謝謝 –