0

I want to export the Web part XML based on GUID to the local. I am trying to export from SharePoint 2013 using CSOM PowerShell. Anyone suggest on this?

function EnsureDirectory($exportFolderPath)
{
  if ( -not (Test-Path $exportFolderPath) ) {New-Item $exportFolderPath -Type Directory | Out-Null}
}
 
function ExportAllWebParts()
{
  $WebURL= ""
  $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
  $Page = $ctx.web.GetFileByServerRelativeUrl("")  
  $ctx.Load($Page)  
  $ctx.ExecuteQuery()
  $wpManager = $Page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
  $webparts = $wpManager.Webparts  
  $ctx.Load($webparts)  
   $ctx.ExecuteQuery()

   if($webparts.Count -gt 0){  
    Write-Host "Looping through all webparts"  
    foreach($webpart in $webparts){
        $exportPath = "" + "\" + $webpart.Title + ".xml"
        $xwTmp = new-object System.Xml.XmlTextWriter($exportPath,$null);
        $xwTmp.Formatting = 1;#Indent
        $wpManager.ExportWebPart($webpart, $xwTmp);
        $xwTmp.Flush();
        $xwTmp.Close();

        #$webpartXmlWriter = New-Object System.Xml.XmlTextWriter($fileName,$null)
        #$webpartXmlWriter.Formatting = [System.Xml.Formatting]::Indented
        #$wpManager.ExportWebPart($webpart,$webpartXmlWriter)
          
        
        }  
              
    }  
}
ExportAllWebParts 
0

Try using below PowerShell code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
function EnsureDirectory($exportFolderPath)
{
  if ( -not (Test-Path $exportFolderPath) ) {New-Item $exportFolderPath -Type Directory | Out-Null}
}
 
function ExportAllWebParts($siteUrl,$pageUrl,$exportFolderPath)
{
  $web = Get-SPWeb $siteUrl
  $wpm = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
 
 EnsureDirectory $exportFolderPath
 
foreach($wp in $wpm.WebParts)
{
  $wp.ExportMode="All";
  $exportPath = $exportFolderPath + "\" + $wp.Title + ".xml"
  $xwTmp = new-object System.Xml.XmlTextWriter($exportPath,$null);
  $xwTmp.Formatting = 1;#Indent
  $wpm.ExportWebPart($wp, $xwTmp);
  $xwTmp.Flush();
  $xwTmp.Close();
}

Inside foreach you can add one more condition to check the GUID of web-part and then you can export your web-part once condition fulfilled.

Source: Export web parts to XML using PowerShell

Alternate solution:

Alternatively you can also use PnP PowerShell to return the web part XML of a web part registered on a site as given in below example:

Get-PnPWebPartXml -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -Identity a2875399-d6ff-43a0-96da-be6ae5875f82

Source: Get-PnPWebPartXml

4
  • due to some constraint pnp we cant use it. But can i get this with CSOM.. I think that could be really good to me.
    – Liz
    Jul 7 '20 at 9:50
  • Yes, check above CSOM code. with some minor changes you can achieve what you want. let me know if it works for you. Jul 7 '20 at 10:04
  • annot find an overload for "ExportWebpart" and the argument count: "2". At C:\:24 char:9 + $wpManager.ExportWebPart($webpart, $xwTmp); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    – Liz
    Jul 7 '20 at 15:03
  • I am getting above error. find the updated code
    – Liz
    Jul 7 '20 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.