I need to add a column to my list via PowerShell; this column must be a positive integer.
The solution:
Here is a simple script to accomplish this:
$site = Get-SPSite "http://my_site_collection"
$web = $site.OpenWeb("my_site")
$list = $web.Lists["my_list"]
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Integer
#The new field is not required, so the value of the third parameter is 0
$spFieldName = "PositiveNo"
$web = $site.OpenWeb("my_site")
$list = $web.Lists["my_list"]
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Integer
#The new field is not required, so the value of the third parameter is 0
$spFieldName = "PositiveNo"
$list.Fields.Add($spFieldName, $spFieldType, 0)
$list.Update()
$list.Update()
#Now that we created a new field, let's change it so that it has a minimal value of 1 and no decimal places
$spField = $list.Fields.GetField($spFieldName)
$spField.MinimumValue = 1
$spField.DisplayFormat = "0"
$spField.Update()
$spField.MinimumValue = 1
$spField.DisplayFormat = "0"
$spField.Update()
#Need to add the new column to the list
$views = $list.Views["All Items"]
$views.ViewFields.Add($spFieldName)
$views.Update()
$views.ViewFields.Add($spFieldName)
$views.Update()
No comments:
Post a Comment