Hacking Sitecore and Life one pipeline at a time!

Value List Field

The need for this module was born while writing Dynamic Sites and realized there was a simple List field type missing from Sitecore: A string value list.

Every list drop-down in Sitecore requires the value to equate to an Item of some kind. This causes the raw value of the field to be a GUID.  Alternatively, there is a Droplist field, that will store the value of an item, the key being that the item has to exist.

But what if I wanted an arbitrary set of values? Like True or False? Or what if I wanted a drop down of the available databases? Or available Sitecore sites? And what if I just wanted it as a string value?

These questions were answered by the creation of the Value List Field. This module installs a field type called Value List. There are TWO methods that the Source of these fields can utilize.

  • Pipe Delimited List of Values
  • Custom Code Executing a Source

value list template view

Pipe Delimited List of Values

This is the easiest use case. Simply set the Field Source to a pipe delimited list of values.

value1|value 2|some other value 3

Custom Code Executing a Source

The more advanced method for creating a value field source is to create a class for it. For a simple example, we want to return values for all of the Sitecore sites available in the system.  The key is implementing the IValueList interface from the module DLL. Then set the Source on the field to the code type, as shown below.

code:SitecoreHacker.Foundation.ValueListField.Fields.Datasources.Inherits, SitecoreHacker.Foundation.ValueListField

using System.Linq;
using Sitecore;
using Sitecore.Sites;
using SitecoreHacker.Foundation.ValueListField.Abstraction;

namespace SitecoreHacker.Foundation.ValueListField.Fields.Datasources
{
    internal class Inherits : IValueList
    {
        public string[] ValueListQuery()
        {
            var sites = SiteManager.GetSites().Select(site => site.Name).ToArray();
            return sites;
        }
    }
}

As shown above, create a class that inherits from IValueList and implement the ValueListQuery(). As long as the code in the method returns an array of strings, then the drop down will populate the values in the field.

value field populated

How To Get

There are a few different ways to get this module:

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,237 other subscribers

Blog Stats

  • 138,855 hits
Follow Sitecore Hacker on WordPress.com
Sitecore® and Own the Experience® are registered trademarks of Sitecore Corporation A/S in the U.S. and other countries.  This website is independent of Sitecore Corporation, and is not affiliated with or sponsored by Sitecore Corporation.