How to create a custom droplist in Sitecore

You could call this how to create a custom drop down, or select list, potentially even picker, but if you’ve ended up here hopefully what your after is a way to create a drop down list in the Sitecore admin populated with options that aren’t from Sitecore.

Fortunately this is quite simple to do by creating a new field type. First we will create the code are new custom field type, then add it to the list of field types in the core db and then use it in a template.

Custom Field Type Code

  1. Either create a new class library or decide on an existing one to put the code for your new field type in.
    I’ve imaginatively gone for one called HiMyNameIsTim.Sitecore.FieldTypes
  2. Add a reference to Sitecore.Kernel (you can get this from the Sitecore NuGet feed)
  3. Create a class for your custom field type
  4. Add the following code, replacing the options with your logic to get the values.
using Sitecore.Web.UI.HtmlControls;

namespace HiMyNameIsTim.Sitecore.FieldTypes
{
    public class SchemeDropList : Control
    {
        protected override void DoRender(System.Web.UI.HtmlTextWriter output)
        {
            output.Write("<select" + ControlAttributes + ">");
            output.Write("<option></option>");

            string s;
            if (base.Value == "1")
                s = "selected";
            else
                s = "";
            output.Write("<option value=\"1\" " + s + ">First</option>");

            if (base.Value == "2")
                s = "selected";
            else
                s = "";
            output.Write("<option value=\"2\" " + s + ">Second</option>");
            
            output.Write("</select>");

            RenderChildren(output);
        }
    }
}

  1. Publish this into your site
  2. Login to the admin and switch to the core db
  3. Navigate to “/sitecore/system/Field types/List Types/” in the tree
  4. Create a new item of type “/sitecore/templates/System/Templates/Template field type”
  5. Populate the assembly and class option with the values for the class you just made. For me this is:
At this point you could populate the control field instead, but that requires creating a config file so unless you have a reason to do that, you might as well just do assembly and class
  1. Switch to the master DB and create a template using your new field type. Here’s mine:
  1. Create a page and you should have a drop down list with the values from your class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s