Introduction
SharePoint Online has gained more popularity with the release of REST API and SPFx. Data operation becomes easy in Client-Side Solution with the help of REST API. All these APIs work under current user permission and there could be a scenario that current users can have access to multiple site collection and we would need to fetch data for cross-site collection.
How we can fetch cross-site collection data is a question, but with the help of PnPJs this question can be easily solved.
Let us look at how we can fetch cross-site collection data using PnPJs in SPFx.
Let us look at how we can fetch cross-site collection data using PnPJs in SPFx.
I hope that your environment for SPFx is up and running.
Step 1 - Create SPFx Solution
Using the below command we can create an SPFx solution
- mkdir dataFromOtherSC
- cd dataFromOtherSC
- yo @microsoft/sharepoint
Use the below values when asked for. We will be using React as our front end framework for creating out SPFx webpart.

Step 2 - Add Reference to PnPJs
To add a reference to PnPJs use the below command.
- npm install @pnp/sp —save
Once the reference is installed we can start the setup. To establish context open the file named:
DataFromOtherScWebPart.ts
Import the reference to PnPJs. To do so add the below line:
- import { sp } from "@pnp/sp/presets/all";
Once the import statement is added we can add the below method to establish context
- protected onInit(): Promise < void > {
- return super.onInit().then(_ => {
- sp.setup({
- spfxContext: this.context
- });
- });
- }


Bhargava ChPosted Jan 9, 2021, 12:04 PM
It is not working for batching.. do we have any other option to do that?