Home > Work > Programmatically setting a field to hidden within a Content Type #SharePoint #SP2010 #in

Programmatically setting a field to hidden within a Content Type #SharePoint #SP2010 #in

At a client recently, my development colleague was struggling to find out how to set a field attached to a custom Content Type in a list to be hidden to aid in document management (so it is not shown in the Document Properties panel in Microsoft Word).

The following post seemed to suggest an unofficial way:

However, we try to keep things in a supported model if possible.

Through further looking he came across the following property:

    After further playing in code, he managed to crack it with the “.hidden” method.
    Code Example:

This code will set the Hidden flag (same as you can through the UI) against the document library. The above iterates through a List of strings which contains the fields i want to hide.

    1 SPContentType docCT = docLib.ContentTypes[0]; 2 3 foreach (string fieldDispName in fieldsToHide) 4 { 5 SPField field = docCT.Fields[fieldDispName]; 6 docCT.FieldLinks[field.Id].Hidden = true; 7 } 8 9 docCT.Update(); 10

NB: Please not that this works because we are using our own content type attached to the library.

Advertisements
  1. February 10, 2011 at 18:03

    Cool. Seems a bit quicker than the unsupported toggling CanToggleHidden internal property w/ reflection at any rate!

  1. February 9, 2011 at 14:48

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: