Skip to content

Instantly share code, notes, and snippets.

@jonschoning
Created August 16, 2011 14:27
Show Gist options
  • Save jonschoning/1149213 to your computer and use it in GitHub Desktop.
Save jonschoning/1149213 to your computer and use it in GitHub Desktop.
Remove-DuplicateWebparts.ps1
$ErrorActionPreference='stop'
function Remove-DuplicateWebparts
{
param([System.String] $siteurl, [System.String] $filename)
$site = new-object Microsoft.SharePoint.SPSite($siteurl)
$items = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::MasterPageCatalog).Items
$webParts = new-object 'System.Collections.Generic.List[string]'
foreach ($item in $items)
{
if ($item.Name -eq $filename)
{
$wpm = $item.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
while ($wpm.WebParts.Count -gt 0)
{
$sb = new-object System.Text.StringBuilder
$xws = new-object System.Xml.XmlWriterSettings
$xw = [System.Xml.XmlWriter]::Create($sb, $xws)
$wp = $wpm.WebParts.Item[0]
$wpm.ExportWebPart($wp, $xw)
$xw.Flush()
$data = [System.Security.Cryptography.MD5]::Create().ComputeHash([System.Text.Encoding.UTF8]::GetBytes($sb.ToString()))
$sBuilder = new-object System.Text.StringBuilder
for($i = 0; ($i -lt $data.Length); $i++) { $sBuilder.Append($data[$i].ToString("x2")) }
$md5Hash = $sBuilder.ToString()
if ($webParts.Contains($md5Hash))
{
write-host '#$wpm.DeleteWebPart($wp) $md5Hash==' $md5Hash
$wpm.DeleteWebPart($wp)
}
else
{
write-host '$webParts.Add(' $md5Hash ')'
$webParts.Add($md5Hash)
}
}
}
}
$site.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment