Get Multiple List Item Collection in single AJAX Call – Sharepoint JSOM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Get Multiple List Item Collection in single AJAX Call – Sharepoint JSOM相关的知识,希望对你有一定的参考价值。

We might have a scenario, where we need to access multiple List to get different result collection.

JSOM allows us to send multiple CAML query in single ajax call – This save us load time, instead of making multiple ajax call we can do that in single ajax call.

function retrieveMultipleListItems() {
    var ctx = SP.ClientContext.get_current();
    var oListTask = ctx.get_web().get_lists().getByTitle(‘Task1‘); // Context of List1
    var oList = ctx.get_web().get_lists().getByTitle(‘Task2‘); // Context of List2
    var queryString = ‘<View><Query><OrderBy><FieldRef Name=‘
    DueDate ‘ Ascending=‘
    true ‘ /></OrderBy></Query></View‘; // CAML Query Sort by Date
    var query = new SP.CamlQuery();
    query.set_viewXml(queryString);
    var taskOpenQuery = ‘<View><Query>  <Where>    <Neq>      <FieldRef Name="Status"  />      <Value Type="Choice">Completed</Value>    </Neq> </Where></Query> </View> ‘ // CAML Query Filter
    var queryTask = new SP.CamlQuery();
    queryTask.set_viewXml(taskOpenQuery);
    this.collListItem1 = oListTask.getItems(query);
    ctx.load(collListItem1); // This contains First List Query  this.collListItem2 = oListTask.getItems(queryTask);
    ctx.load(collListItem2); // This contains Second List Query  
    ctx.executeQueryAsync(Function.createDelegate(this, this.GetMultipleListItemsSuccess), Function.createDelegate(this, this.GetMultipleListItemFailure));
}

function GetMultipleListItemsSuccess() {
    var listItemEnumerator = collListItem1.getEnumerator(); // Get collection of List 1 (collListItem1)
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var id = oListItem.get_item(‘Title‘);
    }

    var listItemEnumerator1 = collListItem1.getEnumerator(); // Get collection of List 2 (collListItem2)
    while (listItemEnumerator1.moveNext()) {
        var oListItem = listItemEnumerator1.get_current();
        var id = oListItem.get_item(‘Title‘);
    }
}

function GetMultipleListItemFailure(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n‘ + args.get_stackTrace());
}

Now, You will get two collection results with single ajax call. We can Process each collection separately using getEnumerator().

 

原文地址:http://techierocks.com/2017/11/get-multiple-list-item-collection-in-single-ajax-call-sharepoint-jsom.html

以上是关于Get Multiple List Item Collection in single AJAX Call – Sharepoint JSOM的主要内容,如果未能解决你的问题,请参考以下文章

用PHP查询特殊字符

PHP SQL SELECT where like search item with multiple words

php获取文件夹下所有文件名

如何自动检查以前检查的项目?

linQ to entities不识别方法Models.Repository.Student get_Item(Int32)'方法

mysql left join to table with multiple rows on min id 以确保返回单行