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

猜你喜欢

转载自www.cnblogs.com/bjdc/p/10943236.html