Get options of a choice field in dynamic module

Posted by Community Admin on 04-Aug-2018 11:12

Get options of a choice field in dynamic module

All Replies

Posted by Community Admin on 05-Sep-2012 00:00

Hi sitefinity team,

Is there any way to get the list of possible values of a choice field of a dynamic module programmatically?
Thanks!

Posted by Community Admin on 05-Sep-2012 00:00

Does this might be an idea?

Something.GetValue<String[]>("YourChoiceFieldName");
          
Markus

Posted by Community Admin on 05-Sep-2012 00:00

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

Posted by Community Admin on 05-Sep-2012 00:00
Posted by Community Admin on 05-Sep-2012 00:00

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.

Posted by Community Admin on 25-Feb-2013 00:00

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

Posted by Community Admin on 26-Feb-2013 00:00

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!

Posted by Community Admin on 06-Mar-2013 00:00

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();
// Note: replace "XXFieldNameXX" and "XXTelerik.Sitefinity.DynamicTypes.Model.Custom.ModuleNameXX"
var field = moduleBuilderManager.Provider.GetDynamicModuleFields().Where(f => f.Name == "XXFieldNameXX" && f.FieldNamespace == "XXTelerik.Sitefinity.DynamicTypes.Model.Custom.ModuleNameXX").Single();
if (field != null)


            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);
           

This thread is closed