Skip to content

Instantly share code, notes, and snippets.

@ktskumar
Created March 8, 2022 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktskumar/c46b886061bc198185c008b6f78be6e1 to your computer and use it in GitHub Desktop.
Save ktskumar/c46b886061bc198185c008b6f78be6e1 to your computer and use it in GitHub Desktop.
Copy web-part from one page to another modern page in SharePoint
/*
Below code uses the PnP JS and helps to copy the webpart from one page to another page in modern SharePoint page
*/
import { sp } from "@pnp/sp/presets/all";
(async () => {
const page = await sp.web.loadClientsidePage("/sites/portal/SitePages/page2.aspx");
const control = page.sections[1].columns[0].getControl(0);
console.log("Page: ", page.sections[1].columns[0].getControl(0));
const targetPage = await sp.web.loadClientsidePage("/sites/portal/SitePages/home.aspx");
targetPage.addSection().addControl(control);
await targetPage.save();
})().catch(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment