How to export all the webpart in a sharepoint online page using Powershell?

Below are the power shell script to export all the web part in a SharePoint Page .

foreach($item in $items)
{
$file = $context.Web.GetFileByServerRelativeUrl($Item[“FileRef”])
if($Item[“FileRef”] -eq “/sites/testwebpartpage.aspx”){
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$context.Load($file)
$webparts = $wpManager.Webparts
$context.Load($webparts)
$context.ExecuteQuery()
$ pelement = $XMLFile.CreateElement(“Page”)
$ pelement .SetAttribute(“id”, $Item[“ID”])
$ pelement .InnerText = $Item[“FileRef”]
$ pelement .AppendChild($PageElement) | Out-Null
foreach($webpart in $webparts)
{
$webPartXml = $wpManager.ExportWebPart($webpart.Id)
$Context.ExecuteQuery()
$ pelement = $XMLFile.CreateElement(“WP”)
$ pelement .InnerText = “”
$ pelement .AppendChild($ pelement ) | Out-Null
}
}

Happy Sharepointing folks 🙂

Leave a Reply

Your email address will not be published.