Tuesday, 17 May 2016

Hidden Power Of Sitecore Dictionary Item

Scenario:

Till the time your Sitecore website is in single language, everything will work smooth. But the moment you are asked to add one more language in existing solution, I am sure you will come across below situation.

You have 90% of content coming through Sitecore which means 90% of your website is content managed.

You have 10% of content is hard coded which contains button text, place holder text etc.. as illustrated in below screens. This will make you worry if you want to add one more language in site and make them content managed.





If you are facing similar situation, then you are at right place.. carry on...

Solution:


Option-1

At first glance, you will jump and start thinking to add fields for all those hard coded value in     respective Sitecore item templates. This solution will fit if those hard coded values are page specific. But think for a while why those values are hard coded? The obvious reason is nature of content. This content might be any of below types:
  • Button text
  • Placeholder text
  • Message text
  • Tooltip text and so on..

Hmm.. now you got the reason why those values are hard coded. You must be thinking now:
  • What to do now? 
  • How to make these hard coded values content manageable? 
  • Where to add them in Sitecore as they are NOT related to particular page template?
  • This content are common and used across the site. 
    • For example: "Find" button is used on many pages of the website.

Option-2 will answer all of your above questions.


Option-2

There is one hidden hero in Sitecore which we rarely use and can save our life in this scenario.
It is Dictionary Item.

1) You can now create dictionary items under /sitecore/system/Dictionary/ for all of above hard coded button, placeholder and message content.



2) Add below piece of code to render content from Dictionary Item based on key.

public static class Helper
{
   public static string GetDictionaryItem(string key)
   {
          if (string.IsNullOrEmpty(key))
              return string.Empty;

            string dictionaryValue = string.Empty;
            dictionaryValue = Translate.Text(key);
            return dictionaryValue;
   }
}

Before:
<button type="button" id="btnFind">Find</button>

After:
<button type="button" id="btnFind">@Helper.GetDictionaryItem("Find Button")</button>


Result:

Above code will now pull content from respective Sitecore dictionary item as shown below.



Advantages of using Sitecore Dictionary Item:
1) You don't need to touch existing templates to add new fields
2) You can use same dictionary content across the site. For example, there are ten places where you have hard coded "Find" button text. You can use same dictionary item at all those ten places.
3) Like other Sitecore item, you can add different language versions of Dictionary items.

Hope you will now take the advantage of Sitecore Dictionary in such scenario.
Keep your Sitecore Journey going on!