如何为现有选择字段添加更多选择

时间:2012-06-04 09:52:14

标签: sharepoint sharepoint-2010

这是我的设置:

我有一个包含此字段的列表(从vs2010创建):

ID="{854D15F6-C70F-4C79-AB59-40C8786EF825}"
 Name="fooSelector"
 Type="Choice"
 Sealed="TRUE"
 Required="TRUE"
 DisplayName="Fruits">
  <CHOICES>
   <CHOICE>Apple</CHOICE>
   <CHOICE>Cherry</CHOICE>
   <CHOICE>Both</CHOICE>
  </CHOICES>
</Field>

我想为此字段添加更多选项。 我尝试使用PowerShell在网上找到两个样本...... 一个)

$spWeb = Get-SPWeb http://intranet/foo

$spList = $spWeb.Lists["Dynamics-Nav List"]
$choices =  New-Object System.Collections.Specialized.StringCollection
 "Fish",
 "Melon"  | ForEach-Object { $choices.Add($_) | Out-Null }

$spList.Fields.Add("Fruits",
 [Microsoft.SharePoint.SPFieldType]::Choice,
 $false,
 $false,
 $choices) 
$spList.Fields[“Fruits”].Update()

B)

$w = Get-SPWeb http://intranet/foo
$l = $w.Lists["Dynamics-Nav List"]
$uf = $l.Fields["Fruits"]
$uf.Choices
$uf.Choices.Add("Fish");
$uf.update()
$l.update();

两者都运行良好而没有错误,之后$ uf.Choices显示刚刚添加的“Fish”选项,但两个更改都没有保存。

我做错了什么?

祝福,  阿明

2 个答案:

答案 0 :(得分:1)

尝试使用powershell

$suryafield = "<Field Type='Choice' DisplayName='Surya' Required='TRUE' Format='Dropdown' Group='Custom Columns'
      FillInChoice='FALSE' StaticName='Surya' Name='Surya'>
            <CHOICES>
                <CHOICE>YES</CHOICE>
                <CHOICE>NO</CHOICE>
            </CHOICES>
           </Field>"
      $web.Fields.AddFieldAsXml($suryafield)

了解更多信息,请参阅此帖子 http://suryapulipati.blogspot.com/2013/08/create-sharepoint-choice-field-site.html

答案 1 :(得分:0)

从代码背后尝试此代码:

string newField = string.Concat("<Field Type='Choice' DisplayName='",
                    "FieldName", "' Name='", "FieldName", "' ",
                    "Format='Dropdown' Required='TRUE' Indexed='TRUE'><CHOICES>",
                    "<CHOICE>1</CHOICE>",
                    "<CHOICE>2</CHOICE>",
                    "<CHOICE>3</CHOICE>",
                    "</CHOICES><Default>1</Default>",
                    "<Description> Status.",
                    "</Description></Field>");

    list.Fields.AddFieldAsXml(newField, true,SPAddFieldOptions.AddFieldToDefaultView);
    Web.Update();