Tags

, , , , ,

Sitecore comes with a list of countries, including a Country two-letter ISO code. This list is available at the node: “/sitecore/system/Settings/Analytics/Lookups/Countries/Afghanistan”. But this list is not very convenient, because it is not multi-language: it only stores the country name in English. And because it is a list of items, its performance could also be improved by making it a NameValueList. This is convenient for example when you want your forms to have a Country Selector field. In this article I will show how a multi-language list of countries could be created and supported in Sitecore.

Country Data List

First we add a template with which the country data will be stored. We name the template “CountryData”. The countries will be stored in a NameValue List field, because this is much faster to retrieve from the database than a list of items. We name the field “CountryDataList”. The country’s two-letter ISO code will be used for the “Name” and the country’s DisplayName will be used as the value.

The Item’s language version will be used to store different lists of countries. So the country data will also be multi-language.

The template’s standard values will be used to add a custom Editor. This Editor will contain a button with which a list of countries from the .Net Globalization namespace could be imported to the Sitecore item. The multilist field “Editors” could be found in the “Appearance” field section. Now we create the custom editor, and add it to the Editors list of the template’s standard values.

Create an “.aspx” page, and call it “CountryImportEditor.aspx”. Add a button, a Literal for showing feedback and an introductory text:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .main {margin: 15px; color: #072D6B; font-family: Franklin Gothic Medium; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="main">
        <p class="text">
            Click here to import the country data in the language of the "CurrentUICulture" of the current installation of the .Net framework: <asp:Literal runat="server" ID="LiteralCurrentLanguage"></asp:Literal>
        </p>
        <asp:Button ID="ButtonMain" runat="server" Text="Start import"/>
        <p class="feedback">
            <asp:Literal runat="server" ID="LiteralFeedback"></asp:Literal>
        </p>
    </div>
    </form>
</body>
</html>

Country Data Import

We also added a Literal to show the current UI culture, from which the countries will be imported. Use it like this:

LiteralCurrentLanguage.Text = Thread.CurrentThread.CurrentUICulture.DisplayName;

Then we add a handler to the button’s click event. In this handler, we will get the current item, in the current item language version (in order to ensure multi-language country data). The we will create a name-value string, and we save it to the field “CountryDataList”.

private void ButtonMain_Click(object sender, EventArgs e)
{
	string countriesRawValue = this.GetCountriesNameValueString();
		// get item in current language
	Language language = Sitecore.Data.Managers.LanguageManager.GetLanguage(Request.QueryString["la"]);
	Item currentItem = Sitecore.Context.Database.GetItem(Request.QueryString["id"], language);

	if (currentItem.Fields["CountryDataList"] != null)
	{
		using (new SecurityDisabler())
		{
			currentItem.Editing.BeginEdit();
			currentItem.Fields["CountryDataList"].Value = countriesRawValue;
			currentItem.Editing.EndEdit();
				LiteralFeedback.Text = "Success!";
		}
	}
	else
	{
		LiteralFeedback.Text = "The field 'CountryDataList' does not exist on the current item.";
	}
}

private string GetCountriesNameValueString()
{
	CultureInfo[] cultureInfos = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

	IEnumerable regionInfos = cultureInfos.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).OrderBy(i => i.DisplayName);

	StringBuilder stringBuilderNameValues = new StringBuilder();

	foreach (RegionInfo region in regionInfos.Distinct())
	{
		// prevent duplicate entries
		if (!stringBuilderNameValues.ToString().Contains(region.TwoLetterISORegionName + "="))
		{
			stringBuilderNameValues.Append(region.TwoLetterISORegionName + "=");
			stringBuilderNameValues.Append(region.DisplayName + "&");
		}
	}
		string countriesRawValue = stringBuilderNameValues.ToString().TrimEnd('&');

	return countriesRawValue;
}

In the Core database, we now add an item of the template “/sitecore/templates/Sitecore Client/Content editor/Editor” to this node: “/sitecore/content/Applications/Content Editor/Editors”. (Or you can put it in a folder). We fill the data of this item, and now it appears in every list of Editors that we access.

Country Data Core

This is it. We can now import a list of countries in the UI Culture of your own environment. Note that this list of countries is not complete. In my English language version of the country list, I was missing Cuba, Andorra and Vatican City. And probably some more. So it is very convenient that we can update and expand our list of countries in Sitecore very easily, because it is a NameValue list.

With this country data, we can do anything. For example, we can use it as the datasource for a dropdownlist in a CountryPicker in the Webforms for Marketers module. How this could be done, I will discuss in my next article.

Advertisements