REST query with two lists

2019-09-01 08:50发布

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' and 'List2')

2条回答
Evening l夕情丶
2楼-- · 2019-09-01 09:26

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.

Example

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

Regular Lists

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

Example:

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.

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));
   }
);
查看更多
萌系小妹纸
3楼-- · 2019-09-01 09:39

If you want to batch your query - you may use javascript client side object model. In that case when you do context.ExecuteQueryAsync() - you query all you defined before at one request.

If you need do it via REST - you can simple do two query async, they will execute in parallel.

查看更多
登录 后发表回答