List items validation in SharePoint 2010
In my last article – you have seen how to provision validation formula on individual column in a list or library. Here I’ll show how you can provision validation formula on the list item itself where you can validate multiple columns and display summary message for the user, similar to below:
To implement validation you must define the validation section in the schema.xml of the list you’re trying to add validation to. Here is the sample of validation formula and a message in a context of the list definition:
<Validation Message=”Something is not quite right with this item”>
=Title=”My desired value”
</Validation>
The highlighted part can be any formula used in calculated field definition, here is more info on various options to define it.
In case you’re wondering, the same techn ique can be used to provision validation on already existing list by using:
SPList list = SPContext.Current.Web.Lists["MyList"];
if (list!=null)
{
list.ValidationFormula = “=Title=’My desired value’”;
list.ValidationMessage =”Something is not quite right with this item”;
}
This validation will be triggered every time the new item is created or existing item changed.
Enjoy!
