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
Need help with this product?

We can help you with Step-by-Step Guide to Copy Pages between SharePoint Online

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

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

Creating Copilot Studio Agents Without an M365 Copilot Premium License

A common assumption when getting started with Copilot Studio is that you need a full Microsoft 365 Copilot Premium license. That assumption is...

5 months ago

Power Automate: Get events (V4) shared mailbox fix

When your flow reads a shared mailbox calendar using the Office 365 Outlook → Get events (V4) connector, it can fail in a way that looks like...

6 months ago

Using Managed Identity with Azure Automation & PnP.PowerShell

How to Set Up a Managed Identity for Azure Automation Runbooks and Use It in PowerShell Scripts with PnP.PowerShell This guide walks you throu...

1 year ago

UPDATED - Set Up Pay-as-you-go M365 Chat & SharePoint Agents

Introduction to Pay-as-You-Go Agents in Microsoft 365 Microsoft has introduced a flexible way for organizations to leverage AI capabilities wi...

1 year ago

How to Use Flexible Sections in SharePoint Pages and News

If you’ve been using SharePoint Online to build pages or share news, you know how important it is to have a layout that works for your c...

1 year ago

What's New in SharePoint Pages

1. New Carousel Layout in the Hero Web Part The Hero Web Part has always been a fundamental element of SharePoint pages. The new Carousel Layo...

1 year ago

Power Platform Solutions Made Simple

Introduction to Power Platform Solutions Power Platform Solutions offer many benefits, especially when moving from standalone flows and apps t...

1 year ago

Use your Bluesky post for comments on your Hugo Blog

Since Bluesky is getting really popular by people I interact with, I decided to switch my blog comments to Bluesky posts. In this blog post, I...

1 year ago

Exploring SharePoint Online's SiteAssets Library

The SiteAssets library in SharePoint Online often seems like a mystery to a lot of users I talk to, but it’s important to understand it ...

1 year ago

Re-creating Multilanguage Links: A Step-by-Step Guide

Maybe you’ve also stumbled upon the issue of missing multilanguage links in your SitePages library in SharePoint Online. This can happen...

1 year ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.