title: How to export XML of webpart on the page
body: <p>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?</p>
  <pre><code>function EnsureDirectory($exportFolderPath)
  {
    if ( -not (Test-Path $exportFolderPath) ) {New-Item $exportFolderPath -Type Directory | Out-Null}
  }
   
  function ExportAllWebParts()
  {
    $WebURL= &quot;&quot;
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
    $Page = $ctx.web.GetFileByServerRelativeUrl(&quot;&quot;)  
    $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 &quot;Looping through all webparts&quot;  
      foreach($webpart in $webparts){
          $exportPath = &quot;&quot; + &quot;\&quot; + $webpart.Title + &quot;.xml&quot;
          $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 
  </code></pre>