Sitecore Dictionary with WebAPI AJAX Calls

Posted by

I recently encountered an interesting scenario when trying to load content from the Sitecore Dictionary in a Sitecore 8.2 Web API AJAX call. The dictionary lookup call Translate.Text() was not working as expected. I quickly realized this was because Sitecore could not determine the correct site context and therefore the correct dictionary domain to find my dictionary item.

So I spent the better part of an hour and came up with a handy helper method to solve this problem.

This method uses the xDB tracker to determine the last page item you visited and then uses that to determine the site context and dictionary domain. Finally it calls Translate.TextByDomain() (instead of Translate.Text()) who’s first parameter is the dictionary domain.

That said, to use this helper method, just simply pass in the dictionary key.

///
<summary>
        /// Gets the dictionary value for the given key.
        /// Useful for AJAX calls where site context is null or incorrect;
        /// This method uses the xDB tracker to determine the most recently visited page which is then used determine the site context.
        /// </summary>

        public static string FindDictionaryValueViaDynamicSiteContext(string key)
        {
            if (!string.IsNullOrWhiteSpace(key))
            {
                // Get the last page the user visited via xdb tracker.
                Page page = Sitecore.Analytics.Tracker.Current.Session.Interaction.Pages.LastOrDefault(p =&gt; p.Item.Id != Guid.Empty);

                if (page != null)
                {
                    // Get the item for the last page the user visited.
                    Item pageItem = Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(page.Item.Id));

                    if (pageItem != null)
                    {
                        // Do not allow site info objects who database is empty/missing, or the "core" database.
                        string[] bannedDatabases = new [] { string.Empty, "core" };

                        // Get the site info for the page.
                        SiteInfo siteInfo = Sitecore.Configuration.Factory.GetSiteInfoList()
                            .FirstOrDefault(info =&gt;
                                !bannedDatabases.Contains(info.Database.ToLowerInvariant())
                                &amp;&amp; info.RootPath.ToLowerInvariant() != "/sitecore/content" // don't allow sites who's root path is EXACTLY "/sitecore/content" .
                                &amp;&amp; pageItem.Paths.FullPath.ToLowerInvariant().StartsWith(info.RootPath.ToLowerInvariant()));

                        if (siteInfo != null)
                        {
                            // Use the correct domain dictionary to translate (lookup in dictionary).
                            return Translate.TextByDomain(siteInfo.DictionaryDomain, TranslateOptions.Default, key);
                        }
                    }
                }
            }

            return key ?? string.Empty;
        }<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s