There are times when you set a field to visible (optional or required) in the GUI, yet it refuses to show up on the list\library. This seems to occur if your content type inherits from a parent that has the field hidden. If you attempt to make the child content type field visible again it doesn’t seem to cascade down into the list instance of the content type and thus your field never shows up on the list.
So you think, “ok, I’ll just use PowerShell”. You’re on the right track, but there is more to it than meets the eye.
if you run the following code:
$spWeb = Get-SPWeb "http://teams.drevdevsp2013.com"; $spList = $spWeb.Lists["My Documents"]; $spField = $spList.Fields["Keywords"] $spField.Hidden=$false;
You are going to get the following error message:
Exception setting “Hidden”: “Cannot change Hidden attribute for this field”
What you don’t realize is that when this particular problem occurs, there is an internal field that you can see in the field xml, but is not part of the regular properties called CanToggleHidden. As it sounds, the purpose of this field is to ensure the field can’t be enabled.
But wait!! There’s hope.
There’s a method that can be used called SetFieldBoolValue. It allows us to modify that value. If you do a little research on it, the method is listed in technet. But it’s one of those: “It’s there but you shouldn’t use it” kind of methods. Well in this case we have to use it. The only way to get to it is via the Reflection namespace as follows:
$spWeb = Get-SPWeb "http://teams.drevdevsp2013.com"; $spList = $spWeb.Lists["My Documents"]; $spField = $spList.Fields["Keywords"] $fieldType = $spField.GetType(); #Access the "hands off method" via reflection namespace $hiddenMethod = $fieldType.GetMethod("SetFieldBoolValue", [System.Reflection.BindingFlags]$([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)); #update the can toggle field using our newly aquired method $hiddenMethod.Invoke($spField, @("CanToggleHidden",$true)); #Now that the hidden toggle is open we can make the field to visible $spField.Hidden=$false; $spField.Update();
Once you have completed the script you will find your field is visible again.
Thanks for reading!
Thank you! So well presented and commented and everything! Very pro!
Quick question. I’m trying to run this and getting an issue on the reflection piece.
“You cannot call a method on a null-valued expression” At line:1 char:87
At which point are you getting the error? When binding the method to an object or when trying to invoke the method? Make sure you have the spField object before you try to invoke. It might not actually be the reflection call that is failing but that your SPField object is null.