Loading...

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

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

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.

Published on:

Learn more
The State of the Microsoft 365 Nation
The State of the Microsoft 365 Nation

Recent content on The State of the Microsoft 365 Nation

Share post:

Related posts

Silently Update SharePoint Metadata with PnP PowerShell

Why Bother with Metadata? Think of metadata as the DNA of your SharePoint content. It tells you who created a document, when it was last touch...

2 years ago

New Editing Options for Image Web Part in SharePoint Online

Adding and Editing Images with the Image Web Part The Image web part simplifies the process of adding visual elements to your SharePoint Onlin...

2 years ago

External User Access Reviews in Office 365

Understanding External User Access Before diving into the reviews, it’s important to understand what external access entails. External u...

2 years ago

Power Automate - How-to Posting on BlueSky and Mastodon

Introduction Do you want to save time and effort by automating your social media posts across different platforms? Do you want to learn how to...

2 years ago

M365 Groups: Set Up a 'No Owner Policy' & why it's Important

Introduction Managing group ownership is crucial for maintaining order and security within an organization in Microsoft 365. A “No Owner...

2 years ago

Social media content creator with AI Promts

Prerequisites For this tutorial you need the following: A premium Power Automate account, e.g. a Power Automate per user plan or an Powerapps...

2 years ago

Change SharePoint group to Security Groups with PowerShell

Automating SharePoint Permissions with PowerShell Managing SharePoint user permissions can be a complex and time-consuming task, especially fo...

2 years ago

Deleting doublettes in SharePoint list with PnP PowerShell

In this blog post, I will break down a PnP PowerShell script that is designed to connect to a SharePoint site, retrieve a list of users from a...

2 years ago

Limit Copilot's access to SharePoint Sites and Content

Why Permissions in Office365 matter for Copilot In the ever changing digital landscape, managing permissions and ensuring data security are pa...

2 years ago

Microsoft Teams Channel Types: How to Choose the Right One

Introduction Microsoft Teams is a powerful collaboration tool that allows you to communicate, share, and work with your team members in a secu...

2 years ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy