SharePoint: Make a list field Required programmatically

sharepointsharepoint-2007sharepoint-2010

I have a list and one of the list field is of managed meta data field type.It has to be a required field on the list.I would not set the Required attribute to true in the XML file, as other lists in the project use it as a optional field.Searched many articles, none of them helped .

Note: Field.Required=true;
Field.Update();
Doesn't make the field as required field
Field is not part of content type

Any ideas on this are appreciated..

Thanks

Best Answer

Is the field a part of a contenttype? In that case you might want to get a reference to the fieldlink and set that as required:

var myField = list.Fields["MyFieldName"];
var ct = list.Contenttypes["MyContentType"];

var fieldLink = ct.FieldLinks[myField];
fieldLink.Required = true;
ct.Update();