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 Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s