Home > sharepoint, sharepoint 2010 > Provisioning validation formula to SharePoint 2010 list field

Provisioning validation formula to SharePoint 2010 list field

February 6th, 2010
Share this on Twitter

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:

validation formula

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

validation message

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.Node.f(’GT’,[SP.Exp.Node.a(0),SP.Exp.Node.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!

Yaroslav Pentsarskyy sharepoint, sharepoint 2010 , ,

You must be logged in to post a comment.