The following code snippet can be used to programmatically column validations.
using System;
using Microsoft.SharePoint;
namespace ValidationSettings
{
internal class Program
{
private static void Main(string[] args)
{
const string siteUrl = "SiteUrl";
const string listName = "Custom List";
FieldValidation(siteUrl, listName, "CID", "=AND([CID]<=50,[CID]>0)", "Please enter a value between 1-50.");
FieldValidation(siteUrl, listName, "EmpId", "=EmpId=INT(EmpId)", "decimals are not allowed.");
FieldValidation(siteUrl, listName, "EmpId", "=EmpId>10", "only EmpId greater than 10");
FieldValidation(siteUrl, listName, "Phone", "=IF(LEN(Phone)>9,TRUE,FALSE)","length greater than 10 digits only");
FieldValidation(siteUrl, listName, "CID", "=[CID]<> 00", "Restricts users not to give CID as 00");
Console.ReadKey();
}
private static void FieldValidation(string siteUrl, string listName, string fieldName, string validationFormula,string validationMessage)
{
using (var spSite = new SPSite(siteUrl))
{
using (SPWeb spWeb = spSite.RootWeb)
{
SPList customList = spWeb.Lists.TryGetList(listName);
if (customList != null)
{
var getListField = customList.Fields.GetField(fieldName) as SPFieldNumber;
if (getListField != null)
{
getListField.ValidationFormula = validationFormula;
getListField.ValidationMessage = validationMessage;
getListField.Update();
Console.WriteLine("Validation created in " + fieldName + "");
}
else
{
Console.WriteLine("Invalid Field Name");
}
}
else
{
Console.WriteLine("Invalid List Name");
}
}
}
}
}
}
Reference:
http://chakkaradeep.com/index.php/sharepoint-2010-list-improvements-column-validation-and-list-validation-settings/
http://adicodes.com/column-validations-and-list-validations-in-sharepoint-2010/
using System;
using Microsoft.SharePoint;
namespace ValidationSettings
{
internal class Program
{
private static void Main(string[] args)
{
const string siteUrl = "SiteUrl";
const string listName = "Custom List";
FieldValidation(siteUrl, listName, "CID", "=AND([CID]<=50,[CID]>0)", "Please enter a value between 1-50.");
FieldValidation(siteUrl, listName, "EmpId", "=EmpId=INT(EmpId)", "decimals are not allowed.");
FieldValidation(siteUrl, listName, "EmpId", "=EmpId>10", "only EmpId greater than 10");
FieldValidation(siteUrl, listName, "Phone", "=IF(LEN(Phone)>9,TRUE,FALSE)","length greater than 10 digits only");
FieldValidation(siteUrl, listName, "CID", "=[CID]<> 00", "Restricts users not to give CID as 00");
Console.ReadKey();
}
private static void FieldValidation(string siteUrl, string listName, string fieldName, string validationFormula,string validationMessage)
{
using (var spSite = new SPSite(siteUrl))
{
using (SPWeb spWeb = spSite.RootWeb)
{
SPList customList = spWeb.Lists.TryGetList(listName);
if (customList != null)
{
var getListField = customList.Fields.GetField(fieldName) as SPFieldNumber;
if (getListField != null)
{
getListField.ValidationFormula = validationFormula;
getListField.ValidationMessage = validationMessage;
getListField.Update();
Console.WriteLine("Validation created in " + fieldName + "");
}
else
{
Console.WriteLine("Invalid Field Name");
}
}
else
{
Console.WriteLine("Invalid List Name");
}
}
}
}
}
}
Reference:
http://chakkaradeep.com/index.php/sharepoint-2010-list-improvements-column-validation-and-list-validation-settings/
http://adicodes.com/column-validations-and-list-validations-in-sharepoint-2010/
No comments:
Post a Comment