Featured image of post Step-by-Step Guide to Copy Pages between SharePoint Online

Step-by-Step Guide to Copy Pages between SharePoint Online

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:

  1. PnP-PowerShell module installed
  2. Appropriate permissions for both source and destination sites

Step-by-Step Breakdown

1. Setting Up Variables

1
2
3
$SourceSiteUrl = "Source Site"
$DestinationSiteUrl = "Destination Site"
$PageIds = @(1, 2, 3)

These variables store important information like authentication details, site URLs, and the IDs of pages to be copied.

2. Connecting to SharePoint Sites

1
2
3
4
5
Connect-PnPOnline -Url ($TenantURL + $SourceSiteUrl) -interactive
$SourceConnection = Get-PnPConnection

Connect-PnPOnline -Url ($TenantURL +$DestinationSiteUrl) -interactive
$DestinationConnection = Get-PnPConnection

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 ($PageId in $PageIds) {
    # Get the page item from the source site
    $PageItem = Get-PnPListItem -List 'SitePages' -Id $PageId -Connection $SourceConnection

    if ($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:

  1. Gets the file reference and name
  2. Determines the corresponding folder in SiteAssets
  3. Copies the SiteAssets folder to the destination site
  4. Exports the page from the source site
  5. Imports the page to the destination site
1
2
3
Copy-PnPFolder -SourceUrl $SourceFolderUrl -TargetUrl $DestinationFolderUrl -Force -Overwrite -Connection $DestinationConnection
Export-PnPPage -Force -Identity $PageNameWithoutExtension -Connection $SourceConnection -out ./page.pnp
Invoke-PnPSiteTemplate -Path ./page.pnp -Connection $DestinationConnection

5. Cleaning Up

Finally, the script disconnects from both SharePoint sites:

1
2
Disconnect-PnPOnline -Connection $SourceConnection
Disconnect-PnPOnline -Connection $DestinationConnection

Complete Script

Here’s the complete script for your reference:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Variables
$SourceSiteUrl = "Source Site"
$DestinationSiteUrl = "Destination Site"
$PageIds = @(1, 2, 3)

    # Connect to the source site
    Write-Host 'Connecting to source site...'
    Connect-PnPOnline -Url ($TenantURL + $SourceSiteUrl) -interactive
    $SourceConnection = Get-PnPConnection

    # Connect to the destination site
    Write-Host 'Connecting to destination site...'
    Connect-PnPOnline -Url ($TenantURL +$DestinationSiteUrl) -interactive
    $DestinationConnection = Get-PnPConnection

    foreach ($PageId in $PageIds) {
        Write-Host "Processing page with ID: $PageId"

        # Get the page item from the source site
        $PageItem = Get-PnPListItem -List 'SitePages' -Id $PageId -Connection $SourceConnection

        if ($null -ne $PageItem) {
            # Get the file reference
            $SourceFileRef = $PageItem.FieldValues.FileRef
            $PageFileName = $PageItem.FieldValues.FileLeafRef

            # Copy the page file to the destination site
            Write-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 site
            Write-Host "Copying folder: $SourceFolderUrl"
            Copy-PnPFolder -SourceUrl $SourceFolderUrl -TargetUrl $DestinationFolderUrl -Force -Overwrite -Connection $DestinationConnection
            Export-PnPPage -Force -Identity $PageNameWithoutExtension -Connection $SourceConnection -out ./page.pnp
            Invoke-PnPSiteTemplate -Path ./page.pnp -Connection $DestinationConnection

        }
        else {
            Write-Host "Page not found with ID: $PageId"
        }
    }

    # Disconnect from the sites
    Disconnect-PnPOnline -Connection $SourceConnection
    Disconnect-PnPOnline -Connection $DestinationConnection

    Write-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.

Reply to ollimenzel's post

With an account on the Fediverse or Mastodon, you can respond to this post. Since Mastodon is decentralized, you can use your existing account hosted by another Mastodon server or compatible platform if you don't have an account on this one.

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.