11 May 2014

Using Dictionary for static string

Just a quick post today to talk abut Dictionaries.

I have seen quite a lots of code where we always have some text floating around in either ascx or cshtml. Usually people tells me that this text will never be updated... Ever... and in that case why should we worry about it. I know it is unlikely but when it does happen and the client wants to update the text then that means deployment: TEST > UAT > PROD. Well I have to admit that when I started with Sitecore 6 years ago, I did not know what to do with those neither so we usually placed them as a field in items but tricky for re-usability :)

Anyway, since 6.6, it does not really take long to cater for it. A simple way is o use the dictionary in Sitecore.

Here is a quick view on how you can setup your text and retrieve it...

You can create a new dictionary item such as:

Also it is better if you create some kind of a struture under Dictionary: A, B, C, D... Now on the code side you can retrieve the value by doing something like:

OK. That is the easiest use.
Now from Sitecore 6.6 there is a concept of domain dictionary which is even better if you have multiple sites running on the same sitecore instance. Indeed, it would be so easier to separate each dictionary for each site. Also, a great thing about domain dictionary is if you have all component as modular: Navigation is one module, banner is another one... then you could even think about having a dictionary per module...

So here we go, the first thing you need to do is to create a Sitecore Dictionary Domain on your website. The template you will use is: /sitecore/templates/System/Dictionary/Dictionary Domain
Under this item, you can create a folder and/or a dictionary item...

Now before being able to use it, you will need to define your dictionary domain for your site. For that, you can open the web.config or your specific config in the include folder. Then locate your site definition. If you look at the definition you will see that you can add a new attribute "dictionaryDomain":

              dictionaryDomain: The default domain to use when looking up dictionary phrases for the website. If a phrase does not exist in
                                this dictionary domain, Sitecore attempts to locate that phrase in the default dictionary domain - 
                                /sitecore/system/Dictionary in the current database. If the phrase cannot be found in the default dictionary
                                domain, Sitecore attempts to locate that phrase in the default dictionary domain in the Core database, if that
                                database exists.
                                You can override the site-specific dictionary domain by passing parameters to the Translate.Text() method.

So go ahead and locate your site entry and add the dictionaryDomain="My Site Dictionary":
On the code side, you can now retrieve the value by using either of the following:

1 comment:

  1. Hello Yann,

    Thanks for the detailed explanation.

    I've configured the domain dictionary in my multi-site Sitecore solution. I am running woth Sitecore.NET 8.2 (rev. 170614).

    If you've noticed the Fallback domain is Driplink field and I've setup respective domain dictionary GUID by accessing raw value and configured same in site definition.

    When I tried to access it using TextByDomain, it's only returning key value not Phrase value.

    Any suggestions?