REST查询有两个列表

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

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

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

链接列表

对于链接列表,您可以指定请求返回其他列表中的投影字段和查找的值。 为此,请在$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); } }); } 

请按照文章使用REST API操作SharePoint托管应用程序中的列表项以获取更多详细信息。

然后,您可以阅读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)); } ); 

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

如果你需要通过REST来做 – 你可以简单地做两个查询异步,它们将并行执行。