SharePoint Online: Copy Site Pages from One Site to Another using PowerShell

Requirement: Copy site pages to another site in SharePoint Online.

SharePoint Online: How to Copy a Page to Another Site Collection?
We wanted to copy a modern page from one site to another site in SharePoint Online. The "Copy to" option doesn't support copying site pages from one site to another.
sharepoint online copy page to another site collection

Luckily we've PnP PowerShell available to copy a modern page to another site:
#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/branding"
$PageName =  "About.aspx"

#Connect to Source Site
Connect-PnPOnline -Url $SourceSiteURL -UseWebLogin

#Export the Source page
$TempFile = [System.IO.Path]::GetTempFileName()
Export-PnPClientSidePage -Force -Identity $PageName -Out $TempFile

#Import the page to destination site
Connect-PnPOnline -Url $DestinationSiteURL -UseWebLogin
Apply-PnPProvisioningTemplate -Path $TempFile
This PowerShell script export-imports a given page along with all its web parts, contents, etc. How about copying all pages from one site to another?

PowerShell to Copy All Site Pages to Another Site in SharePoint Online
To copy all pages to another site collection, use this PowerShell script.
#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/branding"

#Connect to the Source Site
Connect-PnPOnline -Url $SourceSiteURL -UseWebLogin

#Export all pages from the source
$TempFile = [System.IO.Path]::GetTempFileName()
Get-PnPProvisioningTemplate -Out $TempFile -Handlers PageContents -IncludeAllClientSidePages -Force

#Import the page to destination site
Connect-PnPOnline -Url $DestinationSiteURL -UseWebLogin
Apply-PnPProvisioningTemplate -Path $TempFile
This will backup all pages in "Site pages" library to a an XML file with all the artifacts from pages and import to given destination site. We can use this method to  backup-restore site pages within same tenant or between different tenants in SharePoint Online.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.