Provisioning validation formula to SharePoint 2010 list field

SharePoint 2010 has a really cool feature allowing you to create validation formula on the list field and display a message to a user if validation fails. Something I have been using custom Rendering Templates and Event Handlers in MOSS.

Here we`ll take a look at how e can provision such special fields using VisualStudio 2010 features.

First, here is how our formula was defined in a field settings:

Here is the result when the number new item is created:

In your Visual Studio 2010 you can define the field right in the schema.xml of the list definition where your field will appear.

Here is how something like this would look like:

<Field Type=”Number” DisplayName=”TestNumber” ID=”{e47ae7ad-fb63-4ead-a50c-4143f86e727f}” StaticName=”TestNumber” Name=”TestNumber”>
<Validation Message=”Test Number must be greater than 0″ Script=”function(x){return SP.Exp.Calc.valid(SP.Exp.f(‘GT’,[SP.Exp.a(0),SP.Exp.v(0)]),x)}”>
=TestNumber&gt;0
</Validation>
</Field>

The highlighted part defines the formula just like in the UI and pretty much follows calculated field syntax, so you can go pretty complex here.

Good luck!

About the author: Yaroslav Pentsarskyy