Skip to content

Instantly share code, notes, and snippets.

@sweeneyrobb
Created September 4, 2014 05:43
Show Gist options
  • Save sweeneyrobb/2ca905955e50ce600dd6 to your computer and use it in GitHub Desktop.
Save sweeneyrobb/2ca905955e50ce600dd6 to your computer and use it in GitHub Desktop.
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
$wpm.WebParts | ForEach-Object {
$wp = $_
$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();
}
}
# Example: ExportAllWebParts "http://www.contoso.com" "pages/default.aspx" "c:\temp"
ExportAllWebParts "{site-url}" "{rel-page-path}" "{output-dir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment