Sunday, August 4, 2013

PowerShell Scripts to Manage Choice Field in SharePoint

Here are some nifty PowerShell scripts to manage choice field in SharePoint.

How to Add New Choice Field to SharePoint List using PowerShell
Lets use PowerShell to create choice column:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Define URL and List name variables 
$WebURL="http://sharepoint.crescent.com"
$ListName ="Employee Profiles"

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#Add New choice field to the list
$list.Fields.Add("Department", [Microsoft.SharePoint.SPFieldType]::Choice, $false)

#Add Choices to the field
$ChoiceField = $list.Fields.GetField("Department") #Get the field
$ChoiceField.Choices.Add("Sales")
$ChoiceField.Choices.Add("Marketing")

#Commit changes
$ChoiceField.update()

SharePoint PowerShell add value to choice column
Choices can be added from array's also.
$ChoiceField = $list.Fields.GetField("Department") #Get the field
#Add in bulk - Array as Choices
$ChoicesArray = @("Marketing","Production","Operations","HR","IT")
$ChoiceField.choices.addrange($ChoicesArray)

#Commit changes
$ChoiceField.update()

PowerShell to create choice column - Multi-Choice Field (Checkboxes)
#Choices for the column
$Choices = New-Object System.Collections.Specialized.StringCollection;
$Choices.Add("Sales")
$Choices.Add("Purchase")
$Choices.Add("Operations")

#Add Multichoice field
$list.Fields.Add("Department",[Microsoft.SharePoint.SPFieldType]::MultiChoice,$False,$False,$Choices)
New fields added to SharePoint lists will not be included in default view by default! You have to add them explicitly.

Remove choices from the choice field:
#Get the choice field
$ChoiceField = $list.Fields.GetField("Department")

#Remove a Choice from the column
$ChoiceField.Choices.Remove("Sales")
#To remove all choices, use: 
#$ChoiceField.Choices.Clear()

#Commit changes to update choice column
$ChoiceField.update()

Set choice fields settings:
We've added the choice field. Now, we can configure choice field settings via PowerShell.
#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#Get the choice field
$ChoiceField = $list.Fields.GetField("Department")

#Required Field?
$ChoiceField.Required = $true
#Set Default value
$ChoiceField.DefaultValue = "Operations"
#Allow Fill-in Choices
$ChoiceField.FillInChoice = $false
#Field format: such as radio button, drop down list
$ChoiceField.EditFormat = "RadioButtons"

#Commit changes
$ChoiceField.update()
You may get error when the column is "Sealed". Set it to "False" to perform any of the change/delete operations.

Get choice field value:
#Define URL and List name variables 
$WebURL="http://sharepoint.crescent.com/"
$ListName ="user Info"

#Get the 1st item stored in the list
$Item = $list.items[0]

#Get the formatted value of "Department" choice column
Write-host "Choice Field Value:" $item.GetFormattedValue("Department")

Set choice field values:
SharePoint 2010 PowerShell script to add value to choice column 
#Define URL and List name variables 
$WebURL="http://sharepoint.brightpoint.com/sites/Operations/"
$ListName ="List Versions"

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#Add new Get the 1st item stored in the list
$Item = $list.AddItem()
$item["Title"] = "Test001" #Set Mandatory title field
#sharepoint 2010 powershell update choice field
$item["Department"] = "Sales"
$item.Update(

Set Multi-choice field value:
#Add new Get the 1st item stored in the list
$Item = $list.AddItem()
$item["Title"] = "Test009"

#Set Choice values
$choicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$choicevalues.Add("Sales")            
$choicevalues.Add("Purchase")    

$list.Fields["Department"].ParseAndSetValue($item,$choicevalues)

#Commit changes
$item.Update()
To check if a choice field is a multiple choice, use:  SPField.FieldValueType.Name -eq "SPFieldMultiChoiceValue"

for all supported properties of choice field in SharePoint, refer SPFieldChoice reference in MSDN



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...