mercredi 17 novembre 2010

Sharepoint/Snippet– Obtenir la liste des valeurs d’un champ de type Choice

Le contexte :
J’ai déployé un Field dans ma collection de site.
Le type de ce Field est Choice déployé via Feature dont voici le code :
<Field ID="{4AF5A3A9-AECF-4237-8AD5-D74315AB2A42}"
Type="Choice" DisplayName="Mon champ"
Required="FALSE" Format="Dropdown"
FillInChoice="FALSE"
Group="Mon groupe de test"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
StaticName="mon_champ" Name="mon_champ">
<CHOICES>
<CHOICE>Choix 1</CHOICE>
<CHOICE>Choix 2</CHOICE>
<CHOICE>Choix 3</CHOICE>
<CHOICE>Choix 4</CHOICE>
</CHOICES>
</Field>
Je veux récupérer les valeurs de cette liste dans une webpart par exemple :
SPField spField = 
SPContext.Current.Fields
.GetFieldByInternalName("mon_champ");
if (spField != null && spField is SPFieldChoice)
{
SPFieldChoice spfc = spField as SPFieldChoice;
foreach (var item in spfc.Choices.Cast<String>().ToArray())
{
this.Controls.Add(new LiteralControl(item + "<BR/>"));
}
On obtiendra donc :
Choix 1
Choix 2
Choix 3
Choix 4