Introduction

Multiple times we get into a situation where we need to migrate webparts between pages. It's an easy job to add a webpart directly but it become very time consuming when you have to do it for multiple pages manually. In this article, I will discuss how to migrate webparts from one Site collection / Page to another Site collection /Page in SharePoint Online using PnP Online PowerShell. The Same process can be extended over multiple pages if needed.
Same steps can be followed for SharePoint On-premise infrastructure using counter PowerShell commands in Farm.     

Pre-Requisite


Install latest SharePointPnPPowerShellOnline.msi from location https://github.com/SharePoint/PnP-PowerShell/releases
  • Source and Destination Site collection have required webparts
  • Source and Destination Page have same page layout 
Overall process is consisting of 2 steps
  • Download and save webpart details in XML schema in local drive
  • Add all webparts to new Page using locally saved XML schema.

Download XML Schema

First step is to connect to SharePoint Online Site Collection

$credentials = Get-Credential
Connect-PnPOnline -Url $sourceUrl -CreateDrive -Credentials $credentials 


$sourceUrl :
This is location of source SharePoint Online site.

Now we will use "Get-PnpWebPartXml" command of PnP SharePoint Online PowerShell to download all webparts from a specified page. 

  Write-Host "Downloading WebPart Xml.." -ForegroundColor Cyan
      $xml = "<WebParts>"
      Get-PnPWebPart -ServerRelativePageUrl $FileServerRelativeURL -Web $webPath | ForEach-Object {
             $xml += Get-PnPWebPartXml -ServerRelativePageUrl $FileServerRelativeURL -Web $webPath -Identity $_.Id
      }
      $Xml = $xml.replace('<?xml version="1.0" encoding="utf-16"?>','')
      $xml += "</WebParts>"
      $file = $XmlFilelocation + "\webparts.xml";
  Set-Content -Path $file $xml 


Different variables used:
$FileServerRelativeURL : Page relative URL in the Source Site collection e.g. /sites/abc/article.aspx
$webPart: Web Relative URL e.g. /sites/abc
$xmlFileLocation: Local drive location to save the XML file e.g. C:/XML

Above commands will download all webpart Schema and save as webparts.xml in local drive.

XML will look as follows:

Apply XML Schema 

Connect to the Target Site Collection 

$credentials = Get-Credential
Connect-PnPOnline -Url $targetUrl -CreateDrive -Credentials $credentials 


$targetUrl: Target SharePoint Online site url.

Access the XML file from local drive and change references of old site collection in the Schema using 'replace'

$xmlFilePath = $path + "\webparts.xml"
$content = Get-Content $xmlFilePath
[xml]$xmlContent = $content.replace($OldSubSiteName,$NewSubSiteName


$path: Local drive path where we have saved the schema XML : C:\XML
$OldSubSiteName: Old sub site name 
$NewSubSiteName: New sub site name

Second step is to check out the File 

  Write-Host $page
  Write-Host "Checking out "$page -ForegroundColor Gray
  Set-PnPFileCheckedOut -Url $page -Web $subSiteRelativePath 


$page: Relative path of the page where we want to add webparts
$subSiteRelativePath: Relative path of the sub site

Iterate through all webparts in XML, remove if already exist and add again.

Write-Host "Adding Webparts" -ForegroundColor Gray
   $xmlContent.WebParts.WebPart | ForEach-Object {
     Remove-PnpWebPart -ServerRelativePageUrl $page -Web $subSiteRelativePath -Title $_.Title
     Add-PnPWebPartToWebPartPage -Xml $_.OuterXml -ZoneId $_.ZoneId -ServerRelativePageUrl $page -ZoneIndex 0
   }
Write-Host "Added Webparts" -ForegroundColor Green 


Final step is to Check in the page in Major version

Set-PnPFileCheckedIn -Url $page -Web $subSiteRelativePath -CheckinType MajorCheckIn -Comment "Updated webparts"
Write-Host "Checked In "$page -ForegroundColor Gray 

Conclusion

Above I explained a simple process of migration of webparts between pages. We can extend this process and make a  script which will migrate multiple webparts over different Webs/Pages. I have used above script to migrate multiple webparts span across multiple pages in different sub site by modifying the XML elements and parsing accordingly. 
I have uploaded scripts at location


Back to Top