带有两个列表的REST查询 [英] REST query with two lists

查看:87
本文介绍了带有两个列表的REST查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to query two list in the same time?

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

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

url: http://sites.com/url/_api/web/lists/GetByTitle ("List1"和"List2")

url: http://sites.com/url/_api/web/lists/GetByTitle(‘List1' and 'List2')

推荐答案

链接列表

对于链接列表,您可以指定该请求返回其他列表中的投影字段以及Lookups的值.为此,请在$select$expand查询选项中都指定字段名称.

Linked Lists

For Linked Lists you can specify that the request returns projected fields from other lists and the values of Lookups. To do this, specify the field name in both the $select and $expand query options.

示例

假定以下链接列表-EmployeeCompany,其中Employee列表包含对Company列表的查找列:

Assume the following linked lists - Employee and Company, where Employee list contains Lookup column to Company list:

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

常规列表

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

Regular Lists

You need to perform two request since batching is not supported in REST API.

示例:

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

The following example demonstrates how to perform read operation for list items

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);
        }
    });
}

请关注文章

Please follow an article Manipulating list items in SharePoint Hosted Apps using the REST API for a more details.

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

Then you could read list items from Employee and Company as demonstrated below:

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));
   }
);

这篇关于带有两个列表的REST查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆