SharePoint Online: Find Site Column Usage in Lists and Libraries using PowerShell

Requirement: Find a Site Column usage in SharePoint Online Site Collection

SharePoint Online: PowerShell to Get a Site Column's usage in lists and libraries of a Site Collection:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to get a particular Site column usage in Lists and Libraries
Function Find-SPOSiteColumnUsage([String]$SiteURL, [String]$SiteColumnInternalName)
{
    Try{
        Write-host -f Yellow "Processing Site:" $SiteURL

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Get All Lists and Libraries of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Webs)
        $Ctx.Load($Ctx.Web.Fields)
        $Ctx.Load($Ctx.Web.Lists)
        $Ctx.ExecuteQuery()

        #Get the site column
        $SiteColumn = $Ctx.Web.Fields | where {$_.InternalName -eq $SiteColumnInternalName}
        if($SiteColumn -ne $NULL)
        {
            #Loop through each List Field
            ForEach($List in $Ctx.Web.Lists)
            {
                Write-host -f Yellow "`t `t Scanning List $($List.Title)"
                $Ctx.Load($List.Fields)
                $Ctx.ExecuteQuery()

                ForEach($Field in $List.Fields)
                {
                    If($Field.Id -eq $SiteColumn.id)
                    {
                        Write-host -f Green "`t `t Found the Site Column in List '$($List.Title)' at Site '$($SiteURL)'"
                    }
                }
            }
        }

        #Iterate through each subsite of the current web
        foreach ($Subweb in $Ctx.web.Webs)
        {
            #Call the function recursively
            Find-SPOSiteColumnUsage $Subweb.url $SiteColumnInternalName
        }
    }
    Catch {
    write-host -f Red "Error Generating Site Column Usage Report!" $_.Exception.Message
    }
}

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$SiteColumnInternalName="TotalInvestment"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to get the Site Column usage
Find-SPOSiteColumnUsage $SiteURL $SiteColumnInternalName 
This script gets a particular site column's usage in lists and libraries and prints the finding as below:
powershell to find site column usage in sharepoint online
SharePoint Online: Find Site Column Usage in Lists and Libraries using PowerShell SharePoint Online: Find Site Column Usage in Lists and Libraries using PowerShell Reviewed by Salaudeen Rajack on October 29, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.