【问题标题】:REST query with two lists带有两个列表的 REST 查询
【发布时间】:2014-04-09 06:45:35
【问题描述】:

是否可以同时查询两个列表?

网址:http://sites.com/url/_api/web/lists/GetByTitle('List1')

网址:http://sites.com/url/_api/web/lists/GetByTitle(‘List1’和‘List2’)

【问题讨论】:

    标签: jquery rest sharepoint


    【解决方案1】:

    链接列表

    对于Linked Lists,您可以指定请求返回来自其他列表的投影字段和查找的值。为此,请在 $select$expand 查询选项中指定字段名称。

    例子

    假设以下链表 - EmployeeCompany,其中 Employee 列表包含指向 Company 列表的查找列:

    /_api/web/lists/getByTitle('Employee')/items?$select=Title,Company/ID,Company/Title&$expand=Company/ID
    

    常规列表

    您需要执行两个请求,因为 REST API 不支持批处理。

    例子:

    以下示例演示如何对列表项执行读取操作

    function getListItems(listName, siteurl, success, failure) {
        $.ajax({
            url: siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                success(data.d.results);
            },
            error: function (data) {
                failure(data);
            }
        });
    }
    

    请关注文章Manipulating list items in SharePoint Hosted Apps using the REST API了解更多详情。

    然后您可以从EmployeeCompany 中读取列表项,如下所示:

    getListItems('Employee','https://contoso.sharepoint.com',
       function(employeeItems){
           console.log(employeeItems);
    
           getListItems('Company','https://contoso.sharepoint.com',
              function(companyItems){
                console.log(companyItems);
              },
              function(error){
                console.log(JSON.stringify(error));
              }
           );
       },
       function(error){
           console.log(JSON.stringify(error));
       }
    );
    

    【讨论】:

      【解决方案2】:

      如果您想批量查询 - 您可以使用 javascript 客户端对象模型。在这种情况下,当您执行 context.ExecuteQueryAsync() 时 - 您在一次请求中查询之前定义的所有内容。

      如果您需要通过 REST 执行此操作 - 您可以简单地异步执行两个查询,它们将并行执行。

      【讨论】:

      • @stockDevelopers,在 javascript 中,所有对 Web 服务的查询默认都是异步的。您可以使用 jquery $ajax、mquery $ajax 或 sharepoint requestexecutor 来处理 Web 服务请求——它将是异步的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多