November 2007 Entries

Using PowerShell and Reflection to Update CanToggleHidden

Monday, November 12, 2007

If you have SharePoint list that contains a hidden field with the CanToggleHidden field set to True, there's no way for you make the field visible through either the UI or the object model. No way that is, except for to use reflection to reach into the SPField class and have a non-public method do the work for you. This is the situation I found myself in and Colin's solution was perfect, but rather than creating ConsoleApplication617 I thought it would be nice to use PowerShell to do this more interactively. It went a little like this:

   1:  [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
   2:  $site = new-object "Microsoft.SharePoint.SPSite" "http://localhost/applications/ms"
   3:  $web = $site.OpenWeb()
   4:  $list = $web.Lists["Categorizations"]
   5:  $field = $list.Fields["Sequence"]
   6:  $type = $field.GetType()
   7:  $mi = $type.GetMethod("SetFieldBoolValue", [Reflection.BindingFlags]"Instance,NonPublic")
   8:  $mi.Invoke($field, ("CanToggleHidden", $true))
   9:  $field.Hidden = $false
  10:  $field.Update()

(PowerShell's enum syntax is interesting in that it will coerce a comma-separated list of values into OR'd flags as on line 7.)

Add Comment | PowerShell | SharePoint