In this post I will show you how to copy pages and their associated SiteAssets folder from one SharePoint site to another using a PnP-PowerShell script.
Unfortunately you can’t copy pages in SharePoint through the UI. But sometimes you may need to copy pages and their associated SiteAssets from one SiteCollection to another.
This blog post will guide you through a PnP-PowerShell script that automates this process, making it easier and more efficient.
Let’s break down the script and look each step:
Prerequisites
Before running the script, make sure you have:
PnP-PowerShell module installed
Appropriate permissions for both source and destination sites
These lines establish connections to both the source and destination SharePoint sites using the PnP-PowerShell cmdlets.
3. Processing Each Page
The script then loops through each page provided by ID and checks if the page exists in the source site. If the page exists, it proceeds to copy the page and its SiteAssets:
1
2
3
4
5
6
7
8
9
10
11
foreach($PageIdin$PageIds){# Get the page item from the source site$PageItem=Get-PnPListItem-List'SitePages'-Id$PageId-Connection$SourceConnectionif($null-ne$PageItem){# Process the page}else{Write-Host"Page not found with ID: $PageId"}}
4. Copying the Page and Assets
For each page, the script:
Gets the file reference and name
Determines the corresponding folder in SiteAssets
Copies the SiteAssets folder to the destination site
# Variables$SourceSiteUrl="Source Site"$DestinationSiteUrl="Destination Site"$PageIds=@(1,2,3)# Connect to the source siteWrite-Host'Connecting to source site...'Connect-PnPOnline-Url($TenantURL+$SourceSiteUrl)-interactive$SourceConnection=Get-PnPConnection# Connect to the destination siteWrite-Host'Connecting to destination site...'Connect-PnPOnline-Url($TenantURL+$DestinationSiteUrl)-interactive$DestinationConnection=Get-PnPConnectionforeach($PageIdin$PageIds){Write-Host"Processing page with ID: $PageId"# Get the page item from the source site$PageItem=Get-PnPListItem-List'SitePages'-Id$PageId-Connection$SourceConnectionif($null-ne$PageItem){# Get the file reference$SourceFileRef=$PageItem.FieldValues.FileRef$PageFileName=$PageItem.FieldValues.FileLeafRef# Copy the page file to the destination siteWrite-Host"Copying page: $PageFileName"# Remove the filename and extension from the source file reference$TargetPageFileRef=[System.IO.Path]::GetDirectoryName($SourceFileRef)-replace$SourceSiteUrl,$DestinationSiteUrl# Determine the corresponding folder in SiteAssets$PageNameWithoutExtension=[System.IO.Path]::GetFileNameWithoutExtension($PageFileName)$SourceFolderUrl="/sites/$SourceSiteUrl/SiteAssets/SitePages/$PageNameWithoutExtension"$DestinationFolderUrl="/sites/$DestinationSiteUrl/SiteAssets/SitePages"# Copy the folder to the destination siteWrite-Host"Copying folder: $SourceFolderUrl"Copy-PnPFolder-SourceUrl$SourceFolderUrl-TargetUrl$DestinationFolderUrl-Force-Overwrite-Connection$DestinationConnectionExport-PnPPage-Force-Identity$PageNameWithoutExtension-Connection$SourceConnection-out./page.pnpInvoke-PnPSiteTemplate-Path./page.pnp-Connection$DestinationConnection}else{Write-Host"Page not found with ID: $PageId"}}# Disconnect from the sitesDisconnect-PnPOnline-Connection$SourceConnectionDisconnect-PnPOnline-Connection$DestinationConnectionWrite-Host'All pages and assets have been copied.'
This script provides an efficient way to copy SharePoint pages and their associated SiteAssets between sites. By automating this process, you can save time instead of manually recreating pages.
Remember to replace the placeholder values for $SourceSiteUrl and $DestinationSiteUrl with your actual SharePoint environment Url’s before running the script.
If you have problems copying your pages, it might make sense to also give third-party tools like ShareGate or AvePoint a try.
I hope this guide and script helps you to copy your pages and SiteAssets between SharePoint sites.
If you have any questions or suggestions, feel free to leave a comment below.
Comments
You can use your Mastodon account to reply to this post. Learn how this is implemented here.