【发布时间】: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
是否可以同时查询两个列表?
网址:http://sites.com/url/_api/web/lists/GetByTitle('List1')
网址:http://sites.com/url/_api/web/lists/GetByTitle(‘List1’和‘List2’)
【问题讨论】:
标签: jquery rest sharepoint
对于Linked Lists,您可以指定请求返回来自其他列表的投影字段和查找的值。为此,请在 $select 和 $expand 查询选项中指定字段名称。
例子
假设以下链表 - Employee 和 Company,其中 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了解更多详情。
然后您可以从Employee 和Company 中读取列表项,如下所示:
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 执行此操作 - 您可以简单地异步执行两个查询,它们将并行执行。
【讨论】: