Back to Insights

List item validation in SharePoint 2010

In my last article — you have seen how to provision a validation formula on an individual column in a list or library. Here I'll show how you can provision a validation formula on the list item itself, so you can validate multiple columns and display a 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 the 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"

The highlighted part can be any formula used in a calculated field definition. Here is more info on various options to define it. In case you're wondering, the same technique can be used to provision validation on an 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 an existing item changed.