Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library

Problem Statement

 
This article is a continuation of my previous article, Duplicate the Client Side Pages into same Modern SharePoint Online Library Folder where I explained how to duplicate the client side page with all existing controls.
 
In this article, I would like to explain this: If the user makes any changes to duplicate page, how to publish the duplicate client side pages to the original page into same site pages library within the same site collection.
 
As a developer, we have a couple of options to duplicate the page into another modern site collection library or different folder strucutre, but there is no way to copy the pages within the same modern site collection sites library within the same folder structure.
 
Prerequisite Steps
 
Let's create a page and add exisitng below control, or user can add any other available controls. This is just for demo purposes.
  • SharePoint Online PnP PowerShell Overview here
  • Browse the exisitng pages
Home Page looks like this,
 
Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library
 
Duplicate Page looks like this with changes,
  • Add new control
  • Modify the pages control layout
Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library

Approach Overview

 
Key steps to publish duplicate client side page with changes to original Modern Site Collection Site Pages into Same Library Folder.
  1. Use PnP Online to connect to SharePoint Online Modern Site
  2. Export PnP Client Side Pages Command export pages with PnP Provisioning Template
  3. Store it locally.
  4. Apply PnP Provisioning Template store it with different name. 
  1. try   
  2.    {  
  3.   
  4.          $srcUrl = "https://mittal1201.sharepoint.com/sites/commsitehub"   
  5.          Connect-PnPOnline -Url $srcUrl  
  6.   
  7.          $SourcePageName = "home_duplicate"  
  8.          $TargetPageName = "home"  
  9.       
  10.          $tempFile = 'C:\CsharpCorner\'+ $SourcePageName +'.xml'  
  11.          Export-PnPClientSidePage -Force -Identity $SourcePageName -Out $tempFile  
  12.   
  13.          $con = Get-Content $tempFile  
  14.          $sourcepage=$SourcePageName +".aspx"  
  15.          $targetpage=$TargetPageName +".aspx"  
  16.        
  17.          $con | % { $_.Replace($sourcepage,$targetpage) } | Set-Content $tempFile  
  18.          Apply-PnPProvisioningTemplate -Path  $tempFile  
  19.          write-host -ForegroundColor Magenta "Page reverted with name of  " $targetpage      
  20.        
  21.      
  22. }   
  23. catch {  
  24.     Write-Host - ForegroundColor Red 'Error '':'  
  25.     $Error[0].ToString();  
  26.      
  27. }  
Save and run this script.
 

OutPut Steps

 
Applying template to client side pages
 
Export-PnPClientSidePage Cmdlets applying the PnP Provisioning template and export ".xml" file into shared locaiton. Cmdlets referece can be find here
 
Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library
Creation of client side pages
 
Apply-PnPProvisioningTemplate cmdlets will create a new page within the same library from download or exported ".xml" file with provided name i.e. pagename _ duplicate
Cmdlets referece can be find here
 
Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library
 
Final Duplicate Page Outcome
 
Browse the url; i.e., original page. New Client Side Page will be available with all configuration and controls.
 
Publish Duplicate Client Side Page Changes To Original Page Within Same SPO Site Pages Library
 
I hope you have enjoyed and learned something new in this article. Thanks for reading and stay tuned for the next article.