zoukankan      html  css  js  c++  java
  • 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() + '
    ' + 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

  • 相关阅读:
    分享一个详情页
    ES6初探,变量的声明
    ES6初探,什么是ES6
    提问回顾
    个人阅读&个人总结
    结对项目-数独程序扩展
    个人作业Week3-案例分析
    个人作业Week2-代码复审
    个人作业1
    【个人项目】数独
  • 原文地址:https://www.cnblogs.com/bjdc/p/10943236.html
Copyright © 2011-2022 走看看