Find What SharePoint Sites Use a Specific WebPart using PowerShell

I know there are tons of people out there having the same issue. How do you figure out where in a SharePoint environment is a certain web part used. The following PowerShell script prompts you for the Full name of the web part’s type, as well as for the url of the web application to look into. It will scan all publishing and system pages and return a list of all pages where that specific web part is being used! Enjoy.​

function FindWebPart($webPartName) 
{
$webAppUrl = Read-Host “URL of the Web Application”
$webApplication = Get-SPWebApplication $webAppUrl
foreach($site in $webApplication.Sites)
{  
foreach($web in $site.AllWebs) 
{
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) 
{
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList
            foreach ($item in $pages.Items) 
{
                $fileUrl = $webUrl + “/” + $item.File.Url
                $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                $wps = $manager.webparts
foreach($webpart in $wps)
{
if($webPartName -eq $webpart.GetType().ToString())
{
Write-Host “Found” $webpart.GetType().ToString() “at” $web.Url -BackgroundColor Green -ForegroundColor Black
}
}
}
        }
        else
{
            $pages = $null
            $pages = $web.Lists[“Site Pages”]            
if ($pages) 
{                
foreach ($item in $pages.Items) 
{
                    $fileUrl = $webUrl + “/” + $item.File.Url
                    $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                    $wps = $manager.webparts
foreach($webpart in $wps)
{
if($webPartName -eq $webpart.GetType().ToString())
{
Write-Host “Found” $webpart.GetType().ToString() “at” $web.Url -BackgroundColor Green -ForegroundColor Black
}
}
                    }
            }
        }  
$web.Dispose()  
    }
$site.Dispose()
}
}
$webPartName = Read-Host “Name of the webpart”
$row = FindWebPart($webPartName)

Find Where a SharePoint Feature is Activated using PowerShell

I had a situation come up where I need to figure out where, in a web application containing several webs and site collection​s, a specific feature was activated. I developed the following PowerShell script to help automate the process and display exactly where a specific feature is activate. Simply pass it the url of the web application you wish to look in, as well as the id of your feature. The script will automatically determine if your feature is scoped at the site collection or at the web level and will print out on screen a list of all site collections or webs where it is activated. Enjoy!

$myFeature = Get-SPFeature $featureId
if($myFeature.Scope -eq “Site”)
{
foreach($siteCollection in $spApp.Sites)
{
foreach($featureRef in $siteCollection.Features)
{
$curFeature = Get-SPFeature $featureRef.DefinitionId
if($curFeature -eq $myFeature)
{
write-host “Found at site collection:” $siteCollection.Url -ForegroundColor green
}
}
$siteCollection.Dispose()
}
}
else
{
foreach($siteCollection in $spApp.Sites)
{
foreach($web in $siteCollection.AllWebs)
{
foreach($featureRef in $web.Features)
{
$curFeature = Get-SPFeature $featureRef.DefinitionId
if($curFeature -eq $myFeature)
{
write-host “Found at Web:” $web.Url -ForegroundColor green
}
}
$web.Dispose()
}
$siteCollection.Dispose()
}
}​

Changing a SharePoint site Theme using PowerShell

This blog article describes how you can use PowerShell to change the look (theme) of an existing SharePoint site. In order for us to be able to do this, we will need to begin by getting a reference to the SharePoint 2013 Design Catalog. You can obtain this by calling the following line of PowerShell.

$designCatalog = $siteCollection.GetCatalog(“Design”)

Next, we need to decide on what theme to apply. To assist you in choosing what theme to apply, you could use the following PowerShell command to list all existing themes in your SharePoint 2013 environment.

$designCatalog.Items | Format-Table Name

Figure 1 below shows the result of running this command on a plain installation of SharePoint Server 2013 SP1.

ListThemes.png

Figure 1 – Listing all existing SharePoint 2013 Themes on a site collection using PowerShell

We’re almost there. Now that you have obtained a list of all existing themes, you need to get a reference to the ne you wish to use. In our case, we will be using the “Characters” theme for demo purposes. This “theme object”, is nothing more than an SPListItem object exposing various properties that help describe the theme n question. In order for you to apply a theme, you will need to get the properties of this theme object and ass them on to the ApplyTheme method of the site you wish to modify. The following example will take a reference to the root web and apply the characters theme on it using PowerShell.

$theme = $designCatalog.Items | Where{$_.Name -eq “Characters”}

$rootWeb = Get-SPWeb http://localhost

$rootWeb.ApplyTheme($theme[“ThemeUrl”].Split(‘,’)[1].Trim(), $null, $theme[“ImageUrl”].Split(‘,’)[1].Trim(), $true) 

Figure 2 below shows the resulting theme applied on the specified web.

CharactersThemeusingPowerShell.png

Figure 2 – SharePoint web with custom theme using PowerShell