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 Answer
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');
});
- I recommend you to install Chrome SP Editor add-in.
- Then open your SharePoint site with targeted lists.
- Then Open developer tools.
- Go to the SharePoint tab and select PnPJS console in the left menu.
- Copy\Paste my code (don't forget to fix urls) and hit CTRL+D.
- In a console you will see the result.
-
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?– AmitMay 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.– BhargavJun 10, 2019 at 20:42