SharePoint get field by GUID with PowerShell

Once I was looking for a problem in SharePoint, I've spent some time digging the ULS Logs. There was an error with the message "Failed caching field with id '{GUID}'".

The first problem I had was the question: which field?

So I decided to build a script that returns the name of the field.
After I knew the name, I wanted to know where the field is used, so I extended the script. Now it returns the Lists and Content Types where the field is in use.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$web = Get-SPWeb http://YourWebURL/
$fieldId = "Your-Field-GUID"

#Get the field in the web
$fieldInWeb = $web.Fields | Where {$_.Id.ToString().StartsWith($fieldId) }
if($fieldInWeb) {
 Write-Output "'$($fieldInWeb.Title)' in Web: '$($web.Title)'"
}

#Get all ContentTypes in the web containing the field
foreach($ct in $web.ContentTypes) {
 $fieldInCt = $ct.Fields | Where {$_.Id.ToString().StartsWith($fieldId) }
 if($fieldInCt) {
  Write-Output "'$($fieldInCt.Title)' in CT: '$($ct.Name)'"
 }
}

#Get all lists in the web containing the field
foreach($list in $web.Lists) {
 $fieldInList = $list.Fields | Where {$_.Id.ToString().StartsWith($fieldId) }
 if($fieldInList) {
  Write-Output "'$($fieldInList.Title)' in List: '$($list.Title)'"
 }          
}
 
$web.Dispose()

Comments

Popular posts from this blog

How to support multiple languages in WPF