码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-05-29 15:07:07      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:new   can   html   sts   let   type   single   collect   cli   

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

标签:new   can   html   sts   let   type   single   collect   cli   

原文地址:https://www.cnblogs.com/bjdc/p/10943236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!