簡體   English   中英

從多個列表中檢索具有存儲在數組中的URL的列表項

[英]Retrieve list items from multiple lists with urls stored in an array

如何使用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
    );

};

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

//我無法在單個變量中檢索所有列表項,因此無法一起顯示所有列表中的數據

在下面的示例中,我創建一個名為ListDataCollection的JavaScript對象,其中包含一個屬性Lists ,該屬性Lists是對象的數組。

其中每個都包含一個Url屬性,其中包含我要獲取內容的列表的URL。

然后,我遍歷對象數組並為每個URL調用Sharepoint REST api。

每次通話完成時:

  • 我使用對REST api的ajax調用接收的數據在當前對象上創建一個Data屬性。
  • 我還將當前對象的Completed屬性更新為true。

然后,我調用一個名為ifAllCompleted的函數,該函數檢查所有ajax調用是否均已結束。

收到所有數據后,我在瀏覽器控制台上記錄了單詞Completed

至此,您已經有了一個對象數組。 每個對象包含一個Sharepoint列表的數據。

例如 :

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

將包含第一個列表中第一個元素的“ 標題欄”的值。

如果需要,可以使用JavaScript中的concat函數將所有數據合並到一個數組中。

看一下Completed一詞后的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM