Powershell Tip #7 - How to find which sites use a specific webpart in SharePoint

by rahul 9/21/2012 1:14:29 PM

Problem Scenario

You are experiencing a bunch of performance issues in SharePoint. After some analysis, you figure out that it is happening due to a specific web part – call it The_Big_BAD_Webpart. How would you find out which sites use this web part?

Solution

Try the following, during non-peak times, especially if your farm is huge and has thousands of sites.

stsadm -o enumallwebs -includewebparts > C:\Temp\EnumeratedOutput.xml

This command might take a while, and can put a lot of load on your database server. That’s why I would strongly recommend running it on non-peak times for your farm.

This EnumeratedOutput.txt will contain a lot of information. You can use the following powershell script to nail down exactly what you need to find out.

For ex. Find out all URLs that contain this text…

$SourceFile="C:\Temp\EnumAllWebsOutput.xml"
$OutputFile="C:\Temp\OnlySiteURL.txt"
$StringToFind="The_Big_BAD_Webpart"
[xml]$SourceFileInMemory = Get-Content $SourceFile

Add-Content $OutputFile "$StringToFind is used in the following sites..."
foreach($AllWebParts in $SourceFileInMemory.databases.database.site.webs.web.WebParts)
{
    foreach($WebPart in $AllWebParts.ChildNodes)
    {
        if($WebPart.Type -eq "$StringToFind")
        {
            $out = $AllWebParts.ParentNode.Url
            Add-Content $OutputFile $out
        }
    }
} 

Or, something like following to get more details…

$SourceFile="C:\Temp\EnumAllWebsOutput.xml"
$OutputFile="C:\Temp\Information.txt"
$StringToFind="The_Big_BAD_Webpart"
[xml]$SourceFileInMemory = Get-Content $SourceFile

Add-Content $OutputFile "$StringToFind is used in the following sites..."
foreach($AllWebParts in $SourceFileInMemory.databases.database.site.webs.web.WebParts)
{
    foreach($WebPart in $AllWebParts.ChildNodes)
    {
        if($WebPart.Type -eq "$StringToFind")
        {
            $out = "Web id = " + $AllWebParts.ParentNode.id + " " + `
                   "Site id = " + $AllWebParts.ParentNode.ParentNode.ParentNode.id + " " + ` 
                   "Owner = " + $AllWebParts.ParentNode.ParentNode.ParentNode.OwnerLogin + " " + `
                   $AllWebParts.ParentNode.Url
            Add-Content $OutputFile $out
        }
    }
} 

Hope this helps, Wave
Rahul

 


Quote of the day:
I didn't really say everything I said. - Yogi Berra


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2013, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0