Get options of a choice field in dynamic module
Hi sitefinity team,
Is there any way to get the list of possible values of a choice field of a dynamic module programmatically?
Thanks!
That way, you get the values saved of an item.
I want to get all possible values of the choice field.
For example,
I've created a field with three possible values.
1. First choice
2. Second choice
3. Third choice
What I want is to get these values programmatically in order to bind them to a dropdownlist in a custom user control
Dear mgrp
Hi Markus,
The link doesn´t help.
I don´t want to get the value saved for a particular item, I want to get all possible values of the choice field configured in module builder.
Thanks for your time.
Was this problem resolved? I'm trying to do the exact same task. I have a custom field, Gender, and I want to get all the possible values then bind that to a control.
Thanks
Hi Ernest,
I send a support ticket and that was the answer:
var manager = ModuleBuilderManager.GetManager();
var field = manager.Provider.GetDynamicModuleFields().Where(f => f.Name ==
"Location"
&& f.FieldNamespace ==
"CourseType"
).Single();
var options = field.Choices.Trim().Split(
','
);
"CourseType" is the full name of the type which the Location choice field belongs to. You can take it from the code reference of you type.
I hope this helps you!
We're using Sitefinity 5.4.4000.0 SE and the proposed solution did not yield a comma-delimited list of choices as implied in this example, but rather, we got what appears to be choices rendered in XML. In our implementation, we have key=value pairs for all choices.
<choices>
<choice text="Choice1" value="one"></choice>
<choice text="Choice2" value="two"></choice>
<choice text="Choice3" value="three"></choice>
</choices>
Here's the solution I created to extract the actual text and value pairs into a simple NameValueCollection:
var moduleBuilderManager = ModuleBuilderManager.GetManager();
NameValueCollection choices = new NameValueCollection();
XmlDocument xml = new XmlDocument();
xml.LoadXml(((DynamicModuleField)field).Choices);
foreach (System.Xml.XmlElement choice in xml.SelectNodes("/choices/choice"))
choices.Add(choice.Attributes["value"].Value, choice.Attributes["text"].Value);