Spfx webpart to show the list of all webparts

Brass Contributor

Dear Experts,

 

I am struggling to fulfil my requirement is that, I need to create modern SharePoint client webpart (Spfx)

Which renders the list of all webpart, that exists in the current page.

 

Can anybody helps me out regarding he same.

 

Thanks

Asesh

4 Replies

@Asesh Kumar Maity The easiest method would be to use PnPJs Client-side page functionality. Check out the section on "Find Controls"

https://pnp.github.io/pnpjs/sp/docs/client-side-pages/

@Beau Cameron 

 

Is this pnpjs abblicable to classic sharepoint page in online?

Means if I create a SPfx webpart using the pnp, is it valid in Classic SharePoint Page.

 

Thanks

Hi @Asesh Kumar Maity 

no, you can't use in classic page.

btw, you can get all spfx webpart in a moder page using this method. 

It require PnP/PnPJs 

  public async GetAllWebpart(): Promise<any[]> {
    // page file
    const file = sp.web.getFileByServerRelativePath(this.context.pageContext.site.serverRequestPath);
    const page = await ClientSidePage.fromFile(file);

    const wpData: any[] = [];

    page.sections.forEach(section => {
      section.columns.forEach(column => {
        column.controls.forEach(control => {
          var wp = { text: control.data.webPartData.title, key: control.data.webPartData.instanceId };
          wpData.push(wp);
        });

      });
    });
    return wpData;
  }

 

Cheers,

Federico