SharePoint 2010: Interacting with Site Columns using PowerShell
Introduction
As many of us know, one of the core building blocks for string content together within SharePoint is Content Types. These can be used to capture any conceptual business entity and as more and more are created you may eventually find that many Site Columns get shared across Content Types. If these are categorized properly and can be easily found within Site Collection Settings, no problem.
But what happens if these columns aren’t easily found, or if useful columns have been created within custom lists instead? Furthermore, if you need to delete a Site Column but cannot find where it's deployed, what do you do then?
This is a situation that I ran into recently and as such I turned to PowerShell for assistance.
Checking which Lists or Sites are using a Particular Site Column
This first script is handy for finding both the GUID and the URL of your Site Column. The SPField class has a method known as ListsFieldUsedIn, which can be used to retrieve information about which sites and lists a specified field is used. Using the following PowerShell, I queried one of my development sites for the Title field. Note that you can use the column Display Name here; you don’t need access to the Field Name to return a result.
# Add SharePoint Snapin to PowerShell
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$web = Get-SPWeb http://%3c%3cyoursite/
$column = $web.Fields["Title"]
$column.ListsFieldUsedIn()
$web.dispose()
But hang on! What’s this? It’s just a list of IDs! That’s to be expected as what we’re returning here is a GUID for both the Site and the List in which the Site Column is used.
There is a way around this though. Using the select statement on the $web variable we set up earlier we can cleanly. Using our example from earlier we can select both the URL and the ID to be displayed.
Get-SPSite http://engchesqltemp | Get-SPWeb | select URL, id
With this line inserted to the script above, you'll receive something that's a lot more usable.
Targeting Specific Sites for Specific Columns
What if you’re working within an environment that has a lot of Site Collections and you only want to target one? The following PowerShell Script can address that.
# Add SharePoint Snapin to PowerShell
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$site = Get-SPSite("http://yoursite/")
$str = "Category"
foreach($web in $site.AllWebs) {
foreach($list in $web.Lists) {
foreach ($field in $list.Fields) {
if($field.InternalName.Contains($str) -or $field.Title.Contains($str)) {
Write-Host "'$web' web - Found a match in the '$field' field in the '$list' list"
}
}
}
}
$site.dispose()
echo "Finished"
Running this script whilst looking for the column name “Category” on one of development machines returned the following results. You’ll generate a few error messages depending upon your permissions and when comparing against some data types but the script will still run. You could even pipe this out to a file if needed.
Listing Fields Used in a List
Sometimes it's handy to quickly get a list of fields (and their static names and ids) when you're developing and need to reference a field or understand what fields are present on a list. Here's how:
$web = get-spweb http://myweb
$list = $web.Lists["my list"]
$list.Fields | sort Title | FT Title,StaticName,ID -AutoSize
How about type information?
$web = get-spweb http://myweb
$list = $web.Lists["my list"]
$list.Fields | sort Title | FT Title,StaticName,ID,Type -AutoSize
How about, knowing if a field is hidden or not?
$web = get-spweb http://myweb
$list = $web.Lists["my list"]
$list.Fields | sort Hidden,Title | FT Title,StaticName,ID,Type,Hidden -AutoSize
And what getting the value of items field?
$web = get-spweb http://myweb
$list = $web.Lists["my list"]
$items = $list.Items;
$items | sort Title | ft Title,ID -AutoSize
$item = $items[0]
$item["Unique Id"]
...but, what happens when two fields have the same title... use the guid (field ID)!
$item[[Guid]"fa564e0f-0c70-4ab9-b863-0177e6ddd247"]