带有两个列表的REST查询 [英] REST query with two lists
问题描述
是否可以同时查询两个列表?
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.
示例
假定以下链接列表-Employee
和Company
,其中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.
然后,您可以从Employee
和Company
中读取列表项,如下所示:
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屋!