Validation Settings for SharePoint list using PowerShell


In this article we will be seeing about the validation settings for SharePoint list using PowerShell.

Go to List => List Settings => General Settings => Validation Settings.

VShare1.gif

We can add the formula to validate and give some error message.

I have a column "Years of experience" and I am going to add the validation formula as shown in the following

VShare2.gif

Using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ValidationSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {                 
                    SPList list=web.Lists["cl"];
                    list.ValidationFormula = "=AND([Years of Experience]>3,[Years of Experience]<6)";
                    list.ValidationMessage = "Years of exxperience must be between 3-6 years";
                    list.Update();
                }
            }
        }
    }
}


Using PowerShell:

$site=Get-SPSite "http://serverName:1111/"
$web=$site.RootWeb
$list=$web.Lists["cl"];
$list.ValidationFormula = "=AND([Years of Experience]>3,[Years of Experience]<6)"
$list.ValidationMessage = "Years of exxperience must be between 3-6 years"
$list.Update()


List Validation:

VShare3.gif