如何生成报告来枚举出整个sharepoint环境中的每个页面所使用的所有webpart

背景

我的公司的SharePoint环境中购买了大量的第三方webpart,比如Quick Apps, Telerik RadEditor, Nintex Workflow等等。。这样做的好处就是成本相对于雇人开发自定义webpart来的性价比更高,因为找来开发团队可能价格很贵而且开发周期结束之后的长期维护可能会很麻烦。而通过购买第三方公司制作的webpart,不但实现了功能,还能得到长期的技术支持,这样在遇到灾难恢复或者SharePoint升级到新版本的时候,你的维护升级成本相对于雇人写代码来说就会显得相当的便宜。

当然买的webpart多了就会有问题,因为你不知道哪些用的多,哪些用的少,如果不能掌握他们被使用的具体数量,我们就无法决策说那些webpart在合同到期后应该要需要继续使用,而哪些则因为使用的范围太小应该被弃用。

于是我们写了一个不算很短但并不复杂的powershell脚本来找出每个页面所使用的webpart并导出结果,再将结果放到Excel中做出图表方便大家理解。

代码

我们直接看代码。

 param([switch]$help)

 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing") function GetHelp()
{
$HelpText = "This script will list out the webparts type along with the names in publishing pages in all site ."
$HelpText
} function GetWebpartInfoInOnePage($page)
{
$manager = $web.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webCollection = $manager.WebParts
if ($webCollection.Count -ne 0)
{
$pageUrl=$web.Url+'/'+$page
for ($i = 0; $i -lt $webCollection.Count; $i++)
{
$webpartType = $webCollection[$i].GetType().Name
if ($webpartType -eq "ErrorWebPart")
{
$repreWebpartType=$webCollection[$i].RepresentedWebPartType.Name
$realType=$repreWebpartType.SubString($repreWebpartType.LastIndexOfAny(".") + 1)
write-host($i + 1).ToString() " " $pageUrl " " $realType " " $webCollection[$i].Title
}
else
{
write-host($i + 1).ToString() " " $pageUrl " " $webCollection[$i].GetType().Name " " $webCollection[$i].Title
}
}
}
} function RahulPublishingPageWebParts()
{
write-host "This script will enlist the webparts used in all publishing pages "
$allWebapplications = Get-SPWebApplication
foreach($webapp in $allWebapplications)
{
foreach($site in $webapp.Sites)
{
foreach($web in $site.AllWebs)
{
$pagesList=$web.Lists["Pages"]
if($pagesList -ne $null)
{
foreach($item in $pagesList.Items)
{
$page=$item.Url
GetWebpartInfoInOnePage($page)
}
}
$sitePagesList=$web.Lists["Site Pages"]
if($sitePagesList -ne $null)
{
foreach($item in $sitePagesList.Items)
{
$page=$item.Url
GetWebpartInfoInOnePage($page)
}
}
$web.Dispose()
}
$site.Dispose()
}
}
} if ($help)
{
GetHelp;
Continue
} else
{
Start-Transcript -path "result.txt" -force
RahulPublishingPageWebParts
Stop-Transcript
}

这个脚本最后会导出一个列表,将列表中的结果放入Excel在使用PowerPivot做出图表就可以了,这样比较方便省力,效果可以如下图。至于具体的制作powerpivot的方法这里就不说了。

如何生成报告来枚举出整个sharepoint环境中的每个页面所使用的所有webpart

上一篇:html5(二)


下一篇:【Oracle XE系列之一】Windows10_X64环境 安装Oracle XE11gR2 X64数据库