Find Where Site Column Used In Content Type

FindWhereSiteColumnUsedInContentType.ps1

# FindWhereSiteColumnUsedInContentType.ps1: Find and print out all Content Types that
# reference each Site Column listed in the input XML file.
# Syntax example:
# .\FindWhereSiteColumnUsedInContentType.ps1 http://bmphub.int/projects "D:\Scripts\SiteColumns.xml"
#=========Parameter Section=============
param(
    [string] $siteCollectionURL = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "FindWhereSiteColumnUsedInContentType: Processing site collection: " $siteCollectionURL -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteCollectionURL
$rootweb = $site.rootweb
# loop over all webs in the Site Collection
foreach ($web in $site.allwebs)
{
    # get the site column XML from the input XML file
    $XmlFileInput = [xml](Get-Content($xmlFilePath))
    # loop over site columns listed in the input XML file
    foreach( $sitecolumn in $XmlFileInput.SiteColumns.SiteColumn)
    {
        $siteColumnToRemove = $sitecolumn.Name
        $sc = $web.Fields[$siteColumnToRemove]
        if ($sc)
        {
            write-host "  Found Site Column '" $sc.Title "' in gallery" -ForegroundColor Black
       
            foreach ($ct in $web.AvailableContentTypes)
            {
                write-host "    Looking in Content Type '" $ct.Name "'..."   -ForegroundColor Gray
           
                foreach($field in $ct.Fields)
                {
                    if ($field.Id -eq $sc.Id)
                    {                    
                        write-host "      Site Column '" $sc.Title "' used in Content Type '" $ct.Name "' in web '" $web.Url "'" -BackgroundColor Yellow -ForegroundColor Black
                    }
                }
            }
        }
    }
}
Stop-SPAssignment $siteScope
Write-host "FindWhereSiteColumnUsedInContentType: Done"

No comments:

Post a Comment