Skip to main content

Cloud

Hidden Fields… I don’t think so !!

I don’t know how or why but sometimes the fields in a content type can get hidden. And you wont get an option of un-hiding the field using the UI. Actually if you try and un-hide the field using the object model, it wont let you. The CanToggleHidden property will be false. And the object model does not allow you to reset this property.

There is a way to reset this property.

The following code uses reflection to invoke the right method and reset the property. After the code executes the field (SPField) can be be made visible.

   1: //field is an object of the class SPField.
   2: Type type = field.GetType();
   3: MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance);
   4: //Invoke the SetFieldBoolValue on the property CanToggleHidden
   5: mi.Invoke(field, new object[] { "CanToggleHidden", true });
   6: //Now the field's Hidden property can be toggled.
   7: field.Hidden = false;
   8: //Update the field.
   9: field.Update(true);

Similarly sometimes there could be a requirement to toggle the CanBeDeleted property on a SPField object. If you cannot do it using the OB then the code below can be used.

   1: // field is an object of SPField class.
   2: if (!field.CanBeDeleted)
   3: {
   4:     Type type = field.GetType();
   5:     MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance);
   6:     //Invoke the SetFieldBoolValue on CanBeDeleted property.
   7:     mi.Invoke(field, new object[] { "CanBeDeleted", true });
   8:     // Now the field can be deleted.
   9: }                                  
  10: field.Delete();

Share this post :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Amol Ajgaonkar

More from this Author

Follow Us