0

My problem:

The code in solution contains an Enum type that is used to render some view and needs to be configured for different items.

I am looking for a way to create a custom checkbox field to be able to select multiple enum values.

Assumptions:

  • Populate the custom field using Enum values from code
  • Avoid creating items in Sitecore that would reflect this Enum to avoid code duplication
  • Provide an Enum Type by filling the Data Source field in Template.
  • Make it working :)

Is there a simple way to create it?

Best regards

4
  • 1
    Why not just use a multilist or drop link control? It effectively gives you the same data when you read it out without any overhead and maintenance issues with custom fields.
    – Richard Seal
    Sep 16, 2019 at 12:58
  • Could you please provide an example of it ? My assumption is that I have existing Enum Type in my code and want to render control with Enum values. I don't want to create and maintain Sitecore Items that reflects those Enum items. Sep 16, 2019 at 13:00
  • Oh - in that case I misunderstood about the enum type in code. I always put the options as Sitecore items.
    – Richard Seal
    Sep 16, 2019 at 13:01
  • No problem, I haven't make my self clear in a first place. Sep 16, 2019 at 13:08

1 Answer 1

2

I have managed to create a solution for that.

Please find code below:

public class EnumCheckList : Checklist
{
    public string Source { get; set; }

    protected override void OnLoad(EventArgs e)
    {
        if (Sitecore.Context.ClientPage.IsEvent)
        {
            ReadCheckedItemsFromRequest();
        }
        else
        {
            if (Controls.Count != 0 || string.IsNullOrWhiteSpace(Source))
                return;

            try
            {
                var enumType = Type.GetType(Source);
                if (enumType == null)
                    return;

                RenderCheckListItems(enumType);
            }
            catch
            {
                Controls.Add(new ListItem() {Header = "Could not load enum for " + Source});
                return;
            }
        }

        base.OnLoad(e);
    }

    private void RenderCheckListItems(Type enumType)
    {
        foreach (Enum val in Enum.GetValues(enumType))
        {
            var item = new ChecklistItem()
            {
                ID = GetIdForEnum(val, enumType),
                Header = val.GetDescription(),
                Value = val.ToString(),
                Checked = this.Value.ToLower().Contains(val.ToString().ToLower())
            };
            this.Controls.Add(item);
        }
    }

    private void ReadCheckedItemsFromRequest()
    {
        var values = this.Context.Request.Form.AllKeys
            .Where(key => key.HasValue())
            .Where(key => key.Contains(this.ID))
            .Select(GetValueFromID)
            .ToList();

        var value = string.Join(",", values);

        Sitecore.Context.ClientPage.Modified = Value != value;
        if (value != Value)
            this.Value = value;
    }

    private string GetIdForEnum(Enum item, Type enumType)
    {
        return this.ID + "." + enumType.Namespace + "." + enumType.Name + "_" + item;
    }

    private string GetValueFromID(string id)
    {
        return id.Substring(id.IndexOf("_", StringComparison.InvariantCulture) + 1);
    }
}

Hope this will help anyone.

Best regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.