Dictionary lists in Umbraco

Out of the box solution for Dictionary lists in Umbraco

Umbraco implementation of localized values is based on Dictionary which is in settings section of Umbraco back-end. This works pretty fine in case you need to display only string values but does not work localized lists.

For example you might want to have country list for your contact form on your website. Dictionary will not help you in this case. You can make a workaround by storing comma separated values for dictionary values and then split them in runtime, but in case you have long lists like I mentioned above list of countries, this becomes really hard to manage.

For this purpose I used simple solution with list organized XML file.

<?xml version="1.0" encoding="utf-8" ?>
<Resources>
<Language code="en">
<Countries>
<Item value="AF">Afghanistan</Item>
 <Item value="AL">Albania</Item>
      <Item value="DZ">Algeria</Item>
      <Item value="AS">American Samoa</Item>
      <Item value="AD">Andorra</Item>
      <Item value="AO">Angola</Item>
	  .
	  .
	  .
    </Countries>
  </Language>
  <Language code="rs">
    <Countries>
      <Item value="AF">Авганистан</Item>
      <Item value="AL">Албаниа</Item>
      <Item value="DZ">Алжир</Item>
      <Item value="AS">Америка Самоа</Item>
      <Item value="AD">Андора</Item>
      <Item value="AO">Ангола</Item>
	  .
	  .
	  .
    </Countries>
  </Language>
</Resources>	
    

For reading this structure I wrote a small class which returns key-value pair list for requested list name and culture. For better performances list is cached and cache dependency is set on the dictionary list XML file which is stored in ~/Config folder as Resources.xml.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Web.Caching;
namespace Umbraco.Custom
{
    public static class DictionaryList  
    {
        public static List<KeyValuePair<string,string>> GetValues(string languageCode, string list){
            XDocument doc;
            List<KeyValuePair<string,string>> result = new List<KeyValuePair<string,string>>();

                string filePath = "~/config/Resources.xml";
                string physicalFilePath = HttpContext.Current.Server.MapPath(filePath);

                CacheDependency fileDependency = new CacheDependency(physicalFilePath);
                
                string cacheKey = "ae0cefcc-6765-4026-b403-d52bef162280";
                if (HttpContext.Current.Cache[cacheKey] as XDocument == null)
                {
                    doc = XDocument.Load(physicalFilePath);
                    HttpContext.Current.Cache.Insert(cacheKey, doc, fileDependency);
                }
                doc = HttpContext.Current.Cache[cacheKey] as XDocument;
                XElement lang = doc.Element("Resources").Elements("Language").Where(e => e.Attribute("code").Value.Equals(languageCode, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (lang != null)
                {
                    XElement listElement = lang.Element(list);
                    if (listElement != null)
                    {
                        result = listElement.Elements("Item").Select(i => new KeyValuePair<string, string>(i.Attribute("value").Value, i.Value)).ToList();
                    }
                }
            }
            return result;
        }
    }
}
    

Until dictionary list implementation is available in Umbraco I'm using this solution and if you find it useful feel free to use it on your websites.

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

PowerShell

read more

Comments for this article