stockDevelopers
stockDevelopers

Reputation: 53

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

Upvotes: 0

Views: 4666

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

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

Upvotes: 1

Nikolay Zainchkovskiy
Nikolay Zainchkovskiy

Reputation: 474

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.

Upvotes: 0

Related Questions