Forum Discussion

narayanareddy's avatar
narayanareddy
Copper Contributor
Dec 14, 2023

Need sample code to copy site pages from one site colleciton to another site collection using pnp.js

Dear all,

I am using pnp.js version - "@pnp/sp": "^3.21.0". I have a requirement to copy site pages from one site collection to another site collection using SPFX webpart. I would need sample code to achieve this. Please share the sample code. I thank you for your time and help.

5 Replies

  • narayanareddy 

     

    import { spfi } from "@pnp/sp";
    import "@pnp/sp/webs";
    import "@pnp/sp/files";
    
    const sp = spfi(...);
    
    // destination is a server-relative url of a new file
    const destinationUrl = `/sites/dev2/sitePages/Home.aspx`;
    
    await sp.web.getFileByServerRelativePath("/sites/dev/sitePages/Home.aspx").copyByPath(destinationUrl, false, true);
    • narayanareddy's avatar
      narayanareddy
      Copper Contributor

      Dear NicolasKheirallah ,

      Thank you so much for your reply. I used the below code.

      const destinationUrl="/sites/Communication2SiteForMyDevelopmentTesting/SitePages/testinganchortag.aspx";
       await this.sp.web.getFileByServerRelativePath("/sites/TestCommunication/SitePages/testinganchortag.aspx").copyByPath(destinationUrl, false, true);

      I get below Forbidden error message. I am the site collection admin in both the site collections -
      RootSiteurl/sites/TestCommunication/_api/SP.MoveCopyUtil.CopyFileByPath(overwrite=@a1)?@a1=false 403 (Forbidden)

      Please share any suggestion you have for me to get rid of this error. Thank you so much for your help.

Resources