Get choice field from dynamic module

May 27, 2014

Here is a simple way to get the list of possible values of a choice field of a dynamic module from code behind

Code snippet is given below.

var manager = ModuleBuilderManager.GetManager();
//Bind Choice field
var fieldList = manager.Provider.GetDynamicModuleFields().Where(f => f.Name == "YOUR_FIELD_NAME" && f.FieldNamespace == "YOUR_DYNAMICMODULE_NAMESPACE").Single();
var options =fieldList.Choices.Trim().Split(',');
TextReader txtReader = new StringReader(options[0]);
DataSet ds = new DataSet();
XmlReader reader = new XmlTextReader(txtReader);
//You will get list of choice field in xml
//And will convert it into DataTable
if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
    drpSelect.DataSource = ds.Tables[0];
    drpSelect.DataTextField = "text";
    drpSelect.DataValueField = "value";