Tuesday, 5 July 2016

Creating Static Options List for the Sitecore ASP.NET CMS

This blog post provides a custom field type that populates a drop-down in a data template with static list of values using the Sitecore ASP.NET web Content Management System (CMS).
Many of the default data template field types allow the user to select one or more Sitecore items, storing the IDs of those items separated by pipe characters. In the case of Droplist, Sitecore stores the name of the selected item rather than its ID. When you create such fields, you use the Source property to specify the parent or root of the selection list, which must exist or which you must create.
In some cases, you may wish to avoid creating items to populate such lists. You can implement a solution based on the following prototype that lets you use the Source property of the field definition item to pass options to include in the list.
 screen capture of ExplicitLookup list
namespace Sitecore.Sharedsource.Shell.Applications.ContentEditor
{
  using System.Web.UI;
  using Assert = Sitecore.Diagnostics.Assert;
  using SC = Sitecore;
  
  public sealed class ExplicitLookup : SC.Web.UI.HtmlControls.Control
  {
    public ExplicitLookup()
    {
      this.Class = "scContentControl";
      this.Activation = true;
    }
  
    public bool HasPostData
    {
      get;
      set;
    }
  
    public string Source
    {
      get;
      set;
    }
  
    protected override void DoRender(HtmlTextWriter output)
    {
      string err = null;
      output.Write("<select" + this.GetControlAttributes() + ">");
      output.Write("<option value=\"\"></option>");
  
      if (string.IsNullOrEmpty(this.Source))
      {
        err = "No source property specified for field.";
      }
      else
      {
        string[] kvPairs = this.Source.Split('&');
        Assert.IsTrue(
          kvPairs != null && kvPairs.Length > 0,
          "kvPairs");
  
        bool valueFound = string.IsNullOrEmpty(this.Value);
  
        foreach (string kvPair in kvPairs)
        {
          int i = kvPair.IndexOf('=');
          string key; 
          string value; 
  
          if (i < 0)
          {
            key = kvPair;
            value = kvPair;
          }
          else
          {
            key = kvPair.Substring(0, i);
            value = kvPair.Substring(i + 1);
          }
  
          value = SC.Globalization.Translate.Text(value);
          valueFound = valueFound || key == this.Value;
          output.Write(string.Format(
            @"<option value=""{0}"" {1}>{2}</option>",
            key,
            this.Value == key ? " selected=\"selected\"" : string.Empty,
            value));
        }
  
        if (!valueFound)
        {
          err = SC.Globalization.Translate.Text(
            "Value not in the selection list.");
        }
      }
  
      if (err != null)
      {
        output.Write("<optgroup label=\"" + err + "\">");
        output.Write(
          "<option value=\"" + this.Value + "\" selected=\"selected\">" + this.Value + "</option>");
        output.Write("</optgroup>");
      }
  
      output.Write("</select>");
  
      if (err != null)
      {
        output.Write("<div style=\"color:#999999;padding:2px 0px 0px 0px\">{0}</div>", err);
      }
    }
  
    protected override bool LoadPostData(string value)
    {
      this.HasPostData = true;
  
      if (value == null)
      {
        return false;
      }
  
      if (this.GetViewStateString("Value") != value)
      {
        ExplicitLookup.SetModified();
      }
  
      this.SetViewStateString("Value", value);
      return true;
    }
  
    private static void SetModified()
    {
      SC.Context.ClientPage.Modified = true;
    }
  }
}

To register the field type:
  1. Logged in to the Sitecore desktop as an administrator, select the core database using the icon at the bottom right.
  2. Open the Content Editor and navigate to the /sitecore/system/Field types/List Types item.
  3. Insert an item named Explicit Lookup using the System/Templates/Template field type data template.
  4. In the Assembly field in the Data section, enter the name of your assembly (.dll file), without the subdirectory or extension (.dll).
  5. In the Class field in the Data section, enter the full namespace and class name.
  6. Select the master database using the icon at the bottom right.
To use the field type in a data template, set the type of a field to Explicit Lookup and set the Source property of that field to a list of keys separated by ampersand (&) characters.
Yes&No&Undecided
Disregarding internationalization, you can use key=value pairs to control what appears in the UI as opposed to what Sitecore stores:
2=Yes&0=No&1=Undecided
Given this simple implementation, you cannot use equals (=) or ampersand (&) in keys or values. Values default to keys.
This approach may be useful only when the items in your list contain no translated or other data that you could otherwise store in the items selected in a standard Droplink.

No comments:

Post a Comment