1

I need to load all master data from multiple lists and bind it to from in form dropdown. For that I need to call every list separately. Is there any option to call all the list in one query using REST api batch.? I tried to search but Batch requests are available only for Add,Update and Delete. Please help..!

1

1 Answer 1

3

The simplest way is to use PnPJS library for that purpose. $batch itself is a bit complicated when it comes to response parsing, etc. however PnPJS library hides all complicated logic inside.
This is how you can query multiple lists in one HTTP request with PnPJS:

import { sp } from "@pnp/sp";

const batch = sp.web.createBatch();

sp.web.getList("sites/<your site>/Lists/List1").items.inBatch(batch).get()
.then(items1 => {
    console.log(items1);

});
sp.web.getList("sites/<your site>/Lists/List2").items.inBatch(batch).get()
.then(items2 => {
    console.log(items2);

});;

// this line executes actual HTTP request to $batch endpoint
batch.execute()
.then(data => {
    // at this point all requests will be completed
    console.log('done');
});
  1. I recommend you to install Chrome SP Editor add-in.
  2. Then open your SharePoint site with targeted lists.
  3. Then Open developer tools.
  4. Go to the SharePoint tab and select PnPJS console in the left menu.
  5. Copy\Paste my code (don't forget to fix urls) and hit CTRL+D.
  6. In a console you will see the result.
2
  • The data object in batch execute method is undefined. Is it possible to retrieve & utilize items1 & items2 objects together, something like a common object for utilizing the values from both the requests?
    – Amit
    May 22, 2019 at 9:37
  • I am trying to use the same example but instead of multiple lists, I am trying to fetch from top level site and multiple subsites. I am getting data from the top level Sites. But not from the subsites.
    – Bhargav
    Jun 10, 2019 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.