Audit Web Part Locations

Through PowerShell, you can export a csv list of every single app and web part and the URLs where they exist. This is useful for keeping track of where apps are visible and who has access to them.

The script will prompt you for the URL. Enter the entire URL of the top site collection site. For example, http://sp2013/sites/company.

When it prompts you for the folder, just enter /.


Param([Parameter(Mandatory=$true)]
[String]
$Url,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String]
$folder,
[Parameter(Mandatory=$false)]
[String]
$WP
)

if ((gsnp Microsoft.SharePoint.Powershell -EA SilentlyContinue) -eq $null){
asnp Microsoft.SharePoint.Powershell -EA Stop
}

$filename = "WebPartsReport_" + (Get-Date).ToFileTimeUtc().ToString() + ".csv"
$filenamewp = "WebPartsReport_" + $WP.Replace(" ","-") + "_" + (Get-Date).ToFileTimeUtc().ToString() + ".csv"

$logfile = Join-Path $folder $filename
$logfilewp = Join-Path $folder $filenamewp

$urlArray = $Url.Split(",")

$header = "File Url, Web Part Title, Web Part Type, Visible"

ac $logfile $header
ac $logfilewp $header

$logfilecontrol = $null

foreach ($SPsite in $urlArray){
Get-SPSite $SPsite| % {

foreach ($web in $_.AllWebs){

if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)){

$library = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $library.PagesList

foreach ($file in $pages.Items){

$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}

$sitepages = [Microsoft.Sharepoint.Utilities.SpUtility]::GetLocalizedString('$Resources:WikiLibDefaultTitle',"core",$web.UICulture.LCID)

$pages = $null
$pages = $web.Lists[$sitepages]

if ($pages -and $pages.ItemCount -gt 0){

foreach ($file in $pages.Items) {
$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}
}
} else {
$sitepages = [Microsoft.Sharepoint.Utilities.SpUtility]::GetLocalizedString('$Resources:WikiLibDefaultTitle',"core",$web.UICulture.LCID)

$pages = $null
$pages = $web.Lists[$sitepages]

if ($pages){

foreach ($file in $pages.Items) {
$fileUrl = $web.Url + "/" + $file.File.Url

$manager = $file.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);

$webparts = $manager.webparts

$webparts | %{
ac $logfile "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

if ($_.DisplayTitle -match $WP -and -not [string]::IsNullOrEmpty($WP)){
ac $logfilewp "$fileUrl, $($_.DisplayTitle), $($_.GetType().ToString()), $($_.IsVisible)"

$logfilecontrol = 1
}
}
}
}
}
}
}
}

.\notepad.exe $logfile

if ($logfilecontrol -eq 1){
.\notepad.exe $logfilewp
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s