Hi,
i want to modify the "Required" property of a field of a list which is associated to a content type. i've modified the content type and schema of the list, but the "Required" is still set to true, i want it to be optional.
Any idea ?
thanx
Hi,
i want to modify the "Required" property of a field of a list which is associated to a content type. i've modified the content type and schema of the list, but the "Required" is still set to true, i want it to be optional.
Any idea ?
thanx
If you created your list and content type programmatically (using XML files), there are a few places where you need to make the change :
You seem to have done these things correctly. However, the schema.xml of the list is only used when the list is created. Therefore, if you changed the schema.xml and deployed it, but without deleting and re-creating the list, your changes will effectively be useless.
EDIT :
If you can't delete and re-create your list, you will have to write code that will do it programmatically (through a feature or the equivalent). This will do the trick :
using (SPSite site = new SPSite("http://yoursite"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Your List");
if (list != null)
{
SPField fld = list.Fields[SPBuiltInFieldId.RequiredField];
fld.Required = false;
fld.Update();
}
}
}
Try like this:
private void SetFieldRequired(SPList list, string field, string contentType, bool required)
{
SPField fieldInList = list.Fields[field];
fieldInList.Required = required;
fieldInList.Update();
SPField fieldInContentType = list.ContentTypes[contentType].Fields[field];
fieldInContentType.Required = required;
fieldInContentType.Update();
}
Don't forget to add some exception handling.