Archive

Posts Tagged ‘SPContentType’

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

February 9, 2011 2 comments

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.

Advertisement
%d bloggers like this: