Error on copy a page from one sharepoint site to another

Vitor Verdura 96 Reputation points
2021-02-22T15:36:20.777+00:00

Hi all,

I work at a school and have a script to create sites, users and folders automatically. In the finaI steps of the script I need to copy a page from a Sharepoint to another and make it the homepage, but I can't and have been stuck...

Here's what I have so far...

$cred = Get-Credential
Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/sitewiththepagemodel -Credentials  $cred

$tempFile = [System.IO.Path]::GetTempFileName();

Export-PnPPage -Identity "SitePages/Bem-vindo-ao-site-da-Turma.aspx" -Force -Out $tempFile

Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/sitebeingcreated" -Credentials  $cred
Apply-PnPProvisioningTemplate -Path $tempFile

Set-PnPHomePage -RootFolderRelativeUrl SitePages/Bem-vindo-ao-site-da-Turma.aspx

When run I get the error:

Export-PnPPage : The object reference was not defined as an instance of an object
At line:630 char:1
+ Export-PnPPage -Identity "SitePages/Bem-vindo-ao-site-da-Turma.aspx"  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Export-PnPPage], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,PnP.PowerShell.Commands.Provisioning.Tenant.ExportPage

** The part of the error above has been translated

I'm not very good at Powershell so please, be gentle!! ;)

Thanks a lot for your help

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,682 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

Accepted answer
  1. Vitor Verdura 96 Reputation points
    2021-02-28T22:12:50.58+00:00

    I've solved it:

    $cred = Get-Credential
    Connect-PnPOnline -Url https://acme.sharepoint.com/sites/sitewiththepagemodel -Credentials  $cred
    $tempFile = [System.IO.Path]::GetTempFileName();
    
    Export-PnPPage -Identity "Bem-vindo-ao-site-da-Turma.aspx" -Force -Out $tempFile
    
    Connect-PnPOnline -Url "https://acme.sharepoint.com/sites/sitebeingcreated" -Credentials  $cred
    Invoke-PnPSiteTemplate -Path $tempFile
    Set-PnPHomePage -RootFolderRelativeUrl Bem-vindo-ao-site-da-Turma.aspx
    

2 additional answers

Sort by: Most helpful
  1. Li Zhang_MSFT 1,566 Reputation points
    2021-02-23T08:47:27.707+00:00

    Hi @Vitor Verdura ,

    The issue is caused by "SitePages/Bem-vindo-ao-site-da-Turma.aspx".

    Quote from Microsoft, the correct syntax should be:

    Export-PnPPage -Identity Home.aspx  
    

    Please remove SitePages/ like following:

    Export-PnPPage -Identity Bem-vindo-ao-site-da-Turma.aspx -Force -Out $tempFile  
    

    For more information, please refer to:

    https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/export-pnppage?view=sharepoint-ps

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Vitor Verdura 96 Reputation points
    2021-02-23T09:23:11.847+00:00

    Thaks for you answer.

    I already had try that but the error is the same! It's not the solution...