如何從使用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
);
};
//需要執行某些功能格式化每個列表項
//我不能夠取回在一個單一變量的所有列表項,以便能夠從所有列表中顯示的數據一起
非常感謝!我確實通過爲每個列表創建不同的函數來解決我的問題,但我的代碼看起來很糟糕。我會試試這個。 –
我用你的方法解決了我面臨的另一個問題。完美的作品!謝謝 –