Using PowerShell, we’re going to walk through handling a Choice field in a list. Specifically, this is a calendar list using the Category field. This may come in handy if you want to automate changing the values from another data source that BCS can’t connect to, or is too much work to get it to connect. I always prefer using SharePoint’s features, but sometimes we need to stretch it to make it work.
As a heads up, if you remove a value that someone has used in their item, the value will remain in the item UNTIL they come back in to edit the item. Since the value no longer exists in the option list, their value will be lost. However, if you have the option enabled on the field to specify their own value, then the previous value will be saved there.
Let’s get to it, open SharePoint 2010 Management Shell
$web = get-spweb http://site/web $list = $web.lists["Calendar"] $list
I like to run the $list just to ensure we have the list properly. Errors aren’t always displayed in PowerShell. Running $list should return the list name.
$choice = $list.fields["Category"] $choice.choices
Again, make sure we have the right field, calling $choice.choices will list all of the current values. The Choices property is a StringCollection, so use typical commands to add/remove items.
Important. If you’re going to clear items then add items, or after any .update(), you’ll need to get the field again (type in $choice = $list.fields[“Category”] again) to access the new values properly.
remove all items
remove one item
(remember get the field again to add items after clearing it)
add an array of choices
$choiceArray = @("Meeting","Work Hours","Business","Holiday","Get-together") $choice.choices.addrange($choiceArray) $choice.update()
add one at a time
$choice.choices.add("Name") $choice.choices.add("Name Of Another") $choice.update()