Search code examples
sitecoresitecore7.2glass-mapper

Sitecore Glass.Mapper returns null


Suddenly and for no apparent reason a piece of code using Glass.Mapper that always worked fine no longer does so. I am trying to retrieve the Glass wrapper of the root element of my website, which is of template MenuPage:

var root = Sitecore.Context.Database.GetItem("/sitecore/content/ecosystem");

this element is not null. But when I try to retrieve the Glass.Mapper version of it:

var TreeHead = new SitecoreContext().GetItem<MenuPage>(root.ID.Guid.ToString());

This item, that previously was never null, now it is constantly so.

I did read this question on the same subject, and indeed when I inspect the root of my website the language seems to be set to "hr" - a value that I never explicitely set, but it's the name of a first-level child of my website that I previously added, and then removed as it was causing problems.

http://screencast.com/t/aMhy7yl3

Could this have somehow made Sitecore/Glass.Mapper think that "hr" (Croatian) should be the language, thus returning a null item? In which case, how do I change the language back to english?


Solution

  • The issue is due to an incorrect language cookie from your previous visit to "/hr", which Sitecore has interpreted as a request for Croatian. Visit the the page again, even if it does not exists and inspect the cookies and you will see it set as website#lang=hr (in Chrome from the resources tab). Even if the language is not set up on your system, the LanguageResolver processor in httpRequestBegin pipeline will still set that as the Context Language. Since no language version exists for the item, Glass is returning null: https://stackoverflow.com/a/19712345/661447

    The easiest solution is to clear the cookies to solve the problem for now.

    The longer term solution if you want to avoid this completely would require overriding some Sitecore processors, not allowing the language resolver to set the cookie for invalid languages and then throwing a 404 for invalid languages. Some more info here but I think it should go further than what has been discussed: https://sdn.sitecore.net/forum//ShowPost.aspx?PostID=61854