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

背景

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

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

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

代码

我们直接看代码。

 1 param([switch]$help)
 2 
 3 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 4 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
 5 
 6 function GetHelp() 
 7 {
 8     $HelpText =  "This script will list out the webparts type along with the names in publishing pages in all site ."
 9     $HelpText
10 }
11 
12 function GetWebpartInfoInOnePage($page)
13 {
14     $manager = $web.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
15     $webCollection = $manager.WebParts
16     if ($webCollection.Count -ne 0) 
17     {
18         $pageUrl=$web.Url+'/'+$page
19         for ($i = 0; $i -lt $webCollection.Count; $i++) 
20         {
21             $webpartType = $webCollection[$i].GetType().Name
22             if ($webpartType -eq "ErrorWebPart")
23             {
24                 $repreWebpartType=$webCollection[$i].RepresentedWebPartType.Name
25                 $realType=$repreWebpartType.SubString($repreWebpartType.LastIndexOfAny(".") + 1)
26                 write-host($i + 1).ToString() "    " $pageUrl "    " $realType "    " $webCollection[$i].Title
27             }
28             else
29             {
30                 write-host($i + 1).ToString() "    " $pageUrl "    " $webCollection[$i].GetType().Name "    " $webCollection[$i].Title
31             }
32         }
33     }
34 }
35 
36 function RahulPublishingPageWebParts() 
37 {
38     write-host "This script will enlist the webparts used in all publishing pages "
39     $allWebapplications = Get-SPWebApplication
40     foreach($webapp in $allWebapplications) 
41     {
42         foreach($site in $webapp.Sites)
43         {
44             foreach($web in $site.AllWebs)
45             {
46                 $pagesList=$web.Lists["Pages"]
47                 if($pagesList -ne $null)
48                 {
49                     foreach($item in $pagesList.Items) 
50                     {
51                         $page=$item.Url
52                         GetWebpartInfoInOnePage($page)
53                     }
54                 }
55                 $sitePagesList=$web.Lists["Site Pages"]
56                 if($sitePagesList -ne $null)
57                 {
58                     foreach($item in $sitePagesList.Items) 
59                     {
60                         $page=$item.Url
61                         GetWebpartInfoInOnePage($page)
62                     }
63                 }
64                 $web.Dispose()
65             }
66             $site.Dispose()
67         }
68     }
69 }
70 
71 if ($help) 
72 {
73     GetHelp;
74     Continue
75 } else 
76 {
77     Start-Transcript -path "result.txt" -force
78     RahulPublishingPageWebParts
79     Stop-Transcript
80 }

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

原文地址:https://www.cnblogs.com/theSharePointEvents/p/get-all-webparts-used-in-every-page-of-web-application.html