Example Multiple Choice Field

Duncan Koza asked on July 16, 2020 00:14

Hello,

I am about to make a multiple choice field in one of my page types and was wondering if there is an example somewhere so I can see how to pull the values out of the database. I am guessing it will just store all the values in a single text field seperated by a "|" but I am currious to see an example of how to retreive the values.

Recent Answers


Sultan Ahmed answered on July 16, 2020 07:21 (last edited on July 16, 2020 07:33)

If i have understood it correctly, you have a multiple choice field on your page type and you wanted to pull the data out of it. Here is an example you can try to pull the data from multiple choice field from page type.

DataClassInfo classInfo = DataClassInfoProvider.GetDataClass("cms.news");
FormInfo formInfo = new FormInfo(classInfo.ClassFormDefinition);
FormFieldInfo ffi = formInfo.GetFormField("options");
Hashtable options = ffi.Settings;
string valuesWinTextXML = options["options"].ToString();

Also you can go with either custom macro or custom transformation method depending on where you're going to use it. This is where you could parse field value and retrieve appropriate data from database.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 16, 2020 15:15 (last edited on July 16, 2020 15:17)

What Sultan provided is a pretty long way about getting this data out in a transformation. But like most things, there are multiple ways to do one thing. If you're in a transformation you can do something like so:

<script runat="server">
using System.Linq;
public string GetSplitValues()
{
    List<string> values = Eval<string>("ColumnName").Split("|").ToList();
    string returnValue = "";
    foreach (string s in values)
    {
        returnValue += "<li>" + s + "</li>";
    }
    return returnValue;
}
</script>

<ul>
    <%# GetSplitValues() %>
</ul>

Your output would look like the following:

  • List item 1
  • List item 2
  • List item 3

Now if you have actual values or IDs in the list, you'd need to perform a lookup inside that foreach loop. This would then be better as a custom transformation method.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.