0

I'm updating choices in a list field and want them to be stored alphabetically, however when I try to sort the choices I get the following error:

$web = Get-SPWeb "mysite"
$list = $web.GetList("/list/url/AllItems.aspx")
$field = $list.Fields["File Under"]
$field.choices.add("NewItem")
$field.choices = $field.choices | sort

"Choices" is a ReadOnly property.
At line: 1 char:8
+ $field. <<<< choices = $field.choices | sort

Is there a way around this?

0

There is no direct way to do this neither in powershell nor in C# code.Ideally you should sort values while displaying .

Create a temporary array, add all the choice fields, sort it and remove previous values in SharePoint and again add the new values :-P

| improve this answer | |
  • So I'm clear, you're saying I should create a sorted temporary array, then remove all the choices from the field, and re-add the choices from the temporary array in the new order? – bdx Sep 18 '14 at 6:09
  • yes, that's correct. But before implementing this on prod environment. Check what happens to the old records. I have not tried this approach. – Manu Singh Sep 18 '14 at 6:11
0

Actualy you can do somethinf like this:

$field = $list.Fields["Test"] 
 $xmlSource = $field.SchemaXml
 $xmlData = [System.Xml.Linq.XElement]::Parse($xmlSource)
 $choicesParent=$xmlData.Element("CHOICES")
 $allchoices=$choicesParent.Elements("CHOICE")
 $allchoices = $allchoices|Sort-Object -Property Value
 $choicesParent.RemoveAll()
 $allchoices|foreach{$choicesParent.Add($_)}
$field.SchemaXml=$xmlData.ToString()
$field.Update();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.