Belangrijke mededeling over deze pagina:

Deze pagina is niet beschikbaar in het Nederlands en daarom wordt de Engelse versie van het artikel getoond. Dit komt vooral voor bij technische artikelen.

I've created a new field type that allows you to set a C# enumeration as data source for a DropList field control.
To set this up, follow these instructions:

First you switch to the Core database and open the Content Editor.
In there you add a new field type to /sitecore/system/Field types/List Types, calling it EnumList.
See the screenshot for all details.

Now create the C# class for the field type, in my example it's called EnumList.cs

using System;
using Sitecore.Web.UI.HtmlControls;

namespace ParTech.Demo.FieldTypes
{
    public class EnumList : Combobox
    {
        public string Source { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (Controls.Count == 0)
            {
                try
                {
                    Type enumType = Type.GetType(Source);
                    string[] enumValues = Enum.GetNames(enumType);

                    foreach (string v in enumValues)
                    {
                        Controls.Add(new ListItem
                        {
                            ID = v,
                            Header = v,
                            Value = v
                        });
                    }
                }
                catch (Exception)
                {
                    Controls.Add(new ListItem
                    {
                        Header = "Could not load enumeration"
                    });
                }
            }

            base.OnLoad(e);
        }
    }
}

And create an example enumeration:

namespace ParTech.Demo.Enums
{
    public enum Animals
    {
        Cat,
        Dog,
        Horse,
        Pig,
        Cow,
        Bird,
        Chicken
    }
}

Now we can start using our new field type!
Add it to one of your templates, like I did in this example:

The Source field must be set to the fully qualified name of your enumeration.
So in my example this is "ParTech.Demo.Enums.Animals, ParTech.Demo" (the assembly name may be omitted in this case).

If you take a look at the result now, you will see a DropList control with the values from your enumeration.

4 Reacties

  • Door Marc Duiker op 8/21/2015 om 4:26 PM

    I'd like to add that it is very important to add a numbering scheme to the enum values (Cat = 0, Dog = 1 etc) and never change these numbers. Since Sitecore stores the number of the enum value you will run into issues (when not using the numbering) when you decide to update the .Net enum type and insert (or remove) enum values since the Sitecore stored number will then map to different enum value.

  • Door Gustavo op 7/24/2014 om 3:47 AM

    I'm getting null as well, when try to access the value stored in my Enum field

  • Door Ruud van Falier op 5/7/2014 om 10:17 AM

    Hi Marvin,

    You can't use an Editable (FieldRenderer) on these fields.
    To edit them in the Page Editor you will need to use an Edit Frame, just like you'd do with a regular list field.

  • Door Marvin op 5/7/2014 om 9:40 AM

    How do you render the selected value in an Editable? I get a null value.

Plaats een reactie