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:
Are you looking to find where a specific column is used in your SharePoint Online lists and libraries? PowerShell can help! This blog post will show you how to use PowerShell to find site column usage in a SharePoint Online site. This can be helpful for understanding how site columns are used in your environment and identifying any potential issues.
#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:
This can be useful when planning to delete or rename Site Columns or when troubleshooting issues with Site Columns.
Can we generate an inventory report of all SharePoint Online site collections using a specific site column?