Populating Static Content into Droplists in Sitecore

Thursday, May 09, 2013 @ 03:11

By: Jon Upchurch, Senior Developer

There are a lot of ways to present choices to your content authors in the Sitecore Content Editor. I love how you can let them choose from other entities in a specific location by setting the Datasource property of a Treelist, Droplink, TreelistEx, etc. This works really well when you need to attach content to an item that lives at a global level as opposed to adding children to the item itself. The experience is also quite a bit more intuitive to a content author assuming you’ve done adequate groundwork to be sure most of the options they’re likely to need are already covered.

This can be more work than is really required for something as simple as a field like Marital Status with possible choices of "Single", "Married", and "Divorced". Sure, you could create a Data Item for these and put them in a settings or metadata area, but there’s a simpler existing solution. Sitecore Dictionaries provide exactly this functionality out of the box, and are easy to use. When you link a Droplist to a Dictionary Domain, it will automatically use the children as the selectable options and store the selection as a string. This is a light-weight way to provide the contents of a Dropdown box that makes it also maintainable by the users.

This is great, but what happens when you need to populate a list of states, or ISO country names, or languages, or anything else that would be an extensive list that is also static? If you’re anything like me, the thought of manually adding a bunch of items by hand isn’t appealing. So, with this in mind I set out to create a tool that I could use going forward for any such case.

First, since I am using the Glass Sitecore Mapper, I started by adding a model class for DictionaryDomain and DictionaryItem.

[SitecoreClass(TemplateId = "My-Guid-Goes-Here")]

public class DictionaryDomain : GlassBase {}

[SitecoreClass(TemplateId = " My-Guid-Goes-Here ")]

public class DictionaryEntry : GlassBase

{

[SitecoreField]

public virtual string Key { get; set; }

[SitecoreField]

public virtual string Phrase { get; set; }

}

This will allow us to use these items in a strongly typed manner. Here, GlassBase is the base model class that includes important properties like DisplayName, Id, Path, etc. It also includes the code that retrieves the children dynamically as described in this blog post. Note, in the latest verison of Glass, I could omit the [SitecoreField] attribute as Glass is smart enough to try to map anything that has the same name as the field. For more info on getting started with the Glass Sitecore Mapper, check out this post by Joshuwa Jenkins.

Now that we can work with these templates as objects, we need a way to hook into them. My preference in this case is to create a web service, as it requires the least amount of work for something I’m already trying to save time by doing. By using a web service, you can include it in the existing web site project, making use of the existing Glass configuration assuming you’re using Glass (and why wouldn’t you be?). Also, because it has the .asmx extension instead of .aspx, Sitecore isn’t going to intercept the call and try to map it to a content item in your tree. There are other valid solutions to this, but since I wanted to use Glass and save time, this is the one I chose.

Once the service was created, I added the following code and the Web Method that calls it:

 

The first method is the web method itself. It reads the items, in this case state abbreviations, from a text file into an array. The second will take any list of strings and a root DictionaryDomain item reference and add all of the items to the list ignoring any items that might already exist.

Put this into your Sitecore project (don’t forget any bin files) and you’re off and running. You can easily pull PopulateValues() into a helper class and call it from anywhere as I did when this was completed.

Obviously, this approach isn’t limited to just something like a list of states. You could apply this just as easily to an existing product catalog, customer list, Lead Management System/CRM, or anything else you can dream up as long as you can model it in Sitecore and Glass.

The finished product looks something like this: