PowerShell Scripts to Manage Choice Field in SharePoint
PowerShell is a handy way to perform tedious and repetitive tasks in SharePoint. Say, we want to add or remove a choice value in a specific choice column for number of sites, etc.
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:
SharePoint PowerShell Add Choices to Choice Field
Choices can be added from array's also. Here is how to add choices to choice field using PowerShell
PowerShell to create choice column - Multi-Choice Field (Check boxes)
Remove choices from the choice field:
Set Choice fields settings - SharePoint PowerShell to update choice field
We've added the choice field. Now, we can configure choice field settings via PowerShell.
for all supported properties of choice field in SharePoint, refer SPFieldChoice reference in MSDN. To get choice field value or set choice field value using PowerShell, refer: How to Get or Set Choice field values using PowerShell
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] #sharepoint powershell to add choice field $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()This PowerShell adds choice field.
SharePoint PowerShell Add Choices to Choice Field
Choices can be added from array's also. Here is how to add choices to choice field using PowerShell
$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 (Check boxes)
#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") #Get all choices: $ChoiceField.Choices #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 - SharePoint PowerShell to update choice field
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.
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. To get choice field value or set choice field value using PowerShell, refer: How to Get or Set Choice field values using PowerShell
PowerShell Scripts to Manage Choice Field in SharePoint
Reviewed by Salaudeen Rajack
on
August 04, 2013
Rating:

Hello,
ReplyDeletePlease Sir, if I want to have a drop down list of choice and the field must be Required! what should I modify here :
$list.Fields.Add("Department",[Microsoft.SharePoint.SPFieldType]::MultiChoice,$False,$False,$Choices)
Thanks
Here is Syntax:
Deletepublic string Add(
string strDisplayName,
SPFieldType type,
bool bRequired,
bool bCompactName,
StringCollection choices
)
So, use: $list.Fields.Add("Department",[Microsoft.SharePoint.SPFieldType]::MultiChoice,$True,$False,$Choices)
Can I Change Choice Field Type from Radio button to Check Boxes?
ReplyDeleteSure, Here is the Script:
Delete$Field = $List.Fields.GetFieldByInternalName($FieldInternalName)
$Field.Type = [Microsoft.SharePoint.SPFieldType]::MultiChoice
$Field.Update($true)