Column validation in SharePoint List using power shell
-
Thursday, March 01, 2012 6:54 PM
Hi
Can we write power shell script to do column validation?
I want to create a column(Number) which allow only whole number(No negative and decimal values).
Anusha
All Replies
-
Thursday, March 01, 2012 7:20 PM
Try this:
$site = Get-SPSite "http://your_site_collection"
$web = $site.OpenWeb("your_site")
$list = $web.Lists["your_list"]
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Integer
$list.Fields.Add("PositiveNo", $spFieldType, 1)
$list.Update()
$spField = $list.Fields.GetField("PositiveNo")
$spField.MinimumValue = 1
$spField.DisplayFormat = "0"
$spField.Update()
$views = $list.Views["All Items"]
$views.ViewFields.Add("PositiveNo")
$views.Update()When you try to add a negative number, you get a validation error: "The value of this field must be greater than or equal to 1."
-
Thursday, March 01, 2012 9:03 PM
Hi Kiwiboris,
The above script is perfect. Can we do this for already existing column
Anusha
-
Thursday, March 01, 2012 9:07 PM
Hi Kiwiboris,
The above script is perfect. Can we do this for already existing column
Anusha
Yep, as long as that column is Number or Integer. Obtain a reference to the list and run this chunk of code:
$spField = $list.Fields.GetField("ExistingColumnName")
$spField.MinimumValue = 1
$spField.DisplayFormat = "0"
$spField.Update()Boris Velikovich
- Proposed As Answer by Varun Malhotra Thursday, March 01, 2012 9:34 PM
- Marked As Answer by Sasha V Friday, March 02, 2012 1:59 AM
-
Monday, March 05, 2012 5:00 PM
Hi,
How do we create Site Column, which is a Choice type field using powershell and add it to content type? I am able to create text field but not choice field.
Any thoughts??
Anusha
-
Monday, March 05, 2012 6:36 PM
I think you should be able to do it via the choice field type, like this: [Microsoft.SharePoint.SPFieldType]::Choice.
Boris Velikovich
-
Monday, March 05, 2012 6:54 PMok. here is the script for choice field
#Get the site collection and web object
$spWeb = Get-SPWeb http:sitecollection
$choices =
New-Object System.Collections.Specialized.StringCollection
"First Choice",
"Second Choice" | ForEach-Object { $choices.Add($_) | Out-Null }
$spweb.Fields.Add("Choice Field",
[Microsoft.SharePoint.SPFieldType]::Choice,
$false,
$false,
$choices)
$spweb.Fields[“Choice Field”].Description = “My Choice Field”
$spweb.Fields[“Choice Field”].Update()
$field = $spweb.fields.getfield(“Choice Field”)
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype = $spweb.ContentTypes["Article Page"]
$ctype.fieldlinks.add($field)
$ctype.Update($true)
Anusha
- Edited by Sasha V Monday, March 05, 2012 6:54 PM
-
Monday, March 05, 2012 7:13 PMLooks good; does it work?
Boris Velikovich
-
Monday, March 05, 2012 8:41 PMyes. It worked.
Anusha

