Find SharePoint Site Column Usage – Report
When trying to delete a SharePoint site column, it gave me an alert saying “Site columns which are included in content types cannot be deleted. Remove all references to this site column prior to deleting it.”
Alright, Lets use PowerShell to find what lists or content types are using a particular site column, before deleting it.
PowerShell script to Find and delete a site column from Content type
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$web= Get-SPWeb "https://sharepoint.crescent.com/sites/operations"
$ColumnInternalName = "BranchLocation"
#Get All Content Types
$CTypes = $web.site.rootweb.ContentTypes
foreach($ContentType in $CTypes)
{
$FieldInUse = $ContentType.FieldLinks | Where {$_.Name -eq $ColumnInternalName }
if($FieldInUse -ne $null)
{
Write-Host "Found the Column in Content Type:" $ContentType.Name -ForegroundColor DarkGreen
#To Remove the field from content type, uncomment below two lines
#$ContentType.FieldLinks.Delete($ColumnInternalName)
#$ContentType.Update()
}
}
Done! Again when trying to delete the site column, received “This site column will be removed and all list columns which were created from it will be permanently orphaned. Are you sure want to delete this site column?”
Well, What does it means? Site columns are created to minimize duplicates and to provide consistency. So, if we delete a site column, all list/library columns created will continue to hold their values. There will not be any impact on the data. But they’ll become local columns. Alright, Before deleting a site column lets see where it s actually being used? Lets Find all references of our Site Column in a particular site collection using PowerShell.
PowerShell script to Find a Site column usage on Lists
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Site URL
$SiteURL = "https://sharepoint.crescent.com/sites/operations"
#Column to search
$ColumnInternalName = "BranchLocation"
#Get the Web
$Site = Get-SPSite $SiteURL
#Get the Column
$column = $site.RootWeb.Fields.GetFieldByInternalName($ColumnInternalName)
#Find all List usages of the column
$SiteColumnUsages = $column.ListsFieldUsedIn() #Gets the WebID & ListID values
#Get the lists where the site column is being used
foreach( $Usage in $SiteColumnUsages )
{
$Site.AllWebs | foreach {$_.Lists} | where {$_.ID -eq $Usage.ListID } | Select Title, ParentWebURL, RootFolder
}
Write-Host "Checking Lists to Remove the Site column..."
foreach( $Usage in $SiteColumnUsages )
{
#Remove columns from Lists where its used
$List = $Site.AllWebs | foreach {$_.Lists} | where {$_.ID -eq $Usage.ListID }
if($List.Fields.ContainsFieldWithStaticName($ColumnInternalName))
{
$field = $List.Fields.GetFieldByInternalName($ColumnInternalName)
## Uncomment these four lines to actually delete a site column from Lists
#$field.AllowDeletion = $true
#$field.Update()
#$List.Fields.GetFieldByInternalName($ColumnInternalName).Delete()
#$List.Update()
Write-Host "Site column $($ColumnInternalName) has been removed from $($List.RootFolder) at $($List.ParentWeb.URL)"
}
}
Do you have CSOM version ?
Do you have this in a CSOM version?
where did you declare $Usage
No need to declare. Its a object from the collection: $SiteColumnUsages