March 25, 2016 Development Tips

Kentico Quick Tip

By: Laura Frese

It is common for Kentico administrators to limit the field options that editors can select from when creating a new page from a custom page type. In many cases developers need to create filters to allow users to filter by that list of options. In this post I will show you how to access the page type form field information and add it to a drop down list for filtering.

Kentico makes custom development a breeze. You can easily access form field settings using the DataClassInfoProvider as shown below.

    protected string[] GetFormFieldOptions()
        DataClassInfo dci = DataClassInfoProvider.GetDataClassInfo("custom.MyPageTypeName");
        if (dci != null)
            //Get the data from the form field
            FormInfo fi = new FormInfo(dci.ClassFormDefinition);
            FormFieldInfo ffi = fi.GetFormField("Industry");
            string[] industries = ffi.Settings["Options"].ToString().Split('\n'); 
            return industries;
        return null;

Then you simply loop through the array and add the items to a drop down list.

        string[] industries = GetFormFieldOptions();
            if(industries != null){
                ddlIndustry.Visible = true;
                //Add blank field to drop down list
                ListItem li_blank = new ListItem();
                li_blank.Text = "--Select Industry--";
                li_blank.Value = "";
                //Add the industries to the drop down list
                foreach (string industry in industries)
                    ListItem li = new ListItem();
                    li.Text = industry.Trim();
                    li.Value = industry.Trim();

Take a look at Kentico's documentation to learn more about creating custom filters.

Note: If you want to get the options from an alternative form you would use AlternativeFormInfoProvider. 

AlternativeFormInfo afp = AlternativeFormInfoProvider.GetAlternativeFormInfo("customtable.MyTableName.MyAlternativeForm");  
FormInfo fi = new FormInfo(afp.FormDefinition);