More in this section

Forums / Developing with Sitefinity / Get options of a choice field in dynamic module

Get options of a choice field in dynamic module

8 posts, 0 answered
  1. Gema
    Gema avatar
    46 posts
    Registered:
    02 Oct 2009
    05 Sep 2012
    Link to this post
    Hi sitefinity team,

    Is there any way to get the list of possible values of a choice field of a dynamic module programmatically?
    Thanks!
  2. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
    05 Sep 2012
    Link to this post
    Does this might be an idea?

    Something.GetValue<String[]>("YourChoiceFieldName");
              
    Markus
  3. Gema
    Gema avatar
    46 posts
    Registered:
    02 Oct 2009
    05 Sep 2012
    Link to this post
    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

  4. Markus
    Markus avatar
    2763 posts
    Registered:
    25 Nov 2005
  5. Gema
    Gema avatar
    46 posts
    Registered:
    02 Oct 2009
    05 Sep 2012
    Link to this post
    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.
  6. Ernest
    Ernest avatar
    37 posts
    Registered:
    12 Mar 2012
    25 Feb 2013
    Link to this post

    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

  7. Gema
    Gema avatar
    46 posts
    Registered:
    02 Oct 2009
    26 Feb 2013 in reply to Ernest
    Link to this post

    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!

  8. Chris Aybar
    Chris Aybar avatar
    25 posts
    Registered:
    20 Oct 2008
    06 Mar 2013 in reply to Gema
    Link to this post

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

8 posts, 0 answered