Deploying column level validations in a feature

In my last post I talked about date range column validation. However, this referred to directly adding the formula through the user interface. What if you have been a very good SharePoint developer and have provisioned your columns, content ypes, lists etc etc via a feature? Not a problem.

There are a couple of options here. Probably simplest is to include the formula in your field definition XML.


	<Field ID="{4A402010-E7A5-4A91-A536-B522EB624B38}"
		   Type="DateTime"
		   Format="DateOnly"
		   Name="MyReviewDate"
		   StaticName="MyReviewDate"
		   DisplayName="Review Date"
		   Group="My Columns">
		<DefaultFormula>=DATE(YEAR(Today)+1,MONTH(Today),DAY(Today))</DefaultFormula>
		<Validation Message="Review Date must be between one week and three years in the future!">=AND([Review Date]&gt;=TODAY()+7,[Review Date]&lt;=DATE(YEAR(TODAY())+3,MONTH(TODAY()),DAY(TODAY())))</Validation>
	</Field>

This is nice. You will quite likely have this XML definition in your project and so this is a quick and easy way to add the validation. I could not find this documented on MSDN – add a comment if you know differently.

Of course some developers prefer to manipulate things through the API so Microsoft have catered for this. You can manipulate certain properties on the field in your feature receiver. Adding code similar to that below in your feature activated event should do it. Disclaimer! Note I have not explicitly tested this!

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                SPWeb root = site.RootWeb;
                if (root.Fields.Contains(MyFieldID.ReviewDate))
                    SPField  field = root.Fields[MyFieldId.ReviewDate];
                    field.ValidationFormula = "=AND([Review Date]&gt;=TODAY()+7,[Review Date]&lt;=DATE(YEAR(TODAY())+3,MONTH(TODAY()),DAY(TODAY())))";
                    field.ValidationMessage = "Review Date must be between one week and three years in the future!";
                    field.Update();
            }
        }

I like these deployment options as they makes for a wholey reproducible deployment experience. Plus it enforces the column validation but doesn’t preclude a user with appropriate permissions removing the validation should they so wish.

Reference

MSDN – SPField.ValidationFormula

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.validationformula.aspx

MSDN – SPField.ValidationMessage

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.validationmessage.aspx

This entry was posted in SharePoint, SharePoint 2010 and tagged . Bookmark the permalink.

One Response to Deploying column level validations in a feature

  1. Pingback: Date range validation in SharePoint list forms | 3 Guys On SharePoint Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>