R – Hide a content type field programmatically

sharepoint

I've got a field referenced by two content types and I use some code in a feature receiver to hide the title field. One content type inherits from the other but doesn't add anything, only changes the name. They are going to have different workflows attached to them. The problem is that the code only hides the title field on one content type, not the derived one.

SPList members = web.Lists["Inspections"];
SPField titleField = members.Fields["Title"];
titleField.ShowInNewForm = false;
titleField.ShowInEditForm = false;
titleField.Required = false;
titleField.Update();

Best Answer

Updating a field in the list will not do anything to the child content types, seeing as each content type in a list not the actual content type itself, but a "silent" child. (inspect the content type id's of the type in the site settings and that of the "same" content type in the list).

use the Site collection's content type collection instead, change the field and properties and then call the content type object's Update method:

SPContentType.Update(true); // true means you want to push any changes made down to all child content types.