Sitecore 8.2 Language Fallback – Glass Mapper

If you are using Glass Mapper v4.4.0.199 and Sitecore 8.2 with Language Fallback enabled you will notice that SitecoreContext.GetItem, .GetRootItem, etc. will return null if the current item language version doesn’t exist.

ItemNull

By default, Glass Mapper checks to see if a Sitecore item has a version in the language of the item you requested. If you have fallback enabled this check will return null. Here is a solution how to avoid this issue adding a version count disabler and it will return your item fallback item. If you noticed, it uses the Global.asax to handle requests and it’s no longer supported in Sitecore 8.2.

To make it work you have to create two processors, one will be executed when the request begins and the second once the requested is being terminated:

public class VersionCountDisablerBegin : HttpRequestProcessor
{
	public override void Process(HttpRequestArgs args)
	{
		Assert.ArgumentNotNull(args, "args");

		HttpContext.Current.Items["VersionCountDisabler"] = new VersionCountDisabler();
	}
}
public class VersionCountDisablerEnd : HttpRequestProcessor
{
	public override void Process(HttpRequestArgs args)
	{
		Assert.ArgumentNotNull(args, "args");

		var disabler = HttpContext.Current.Items["VersionCountDisabler"] as VersionCountDisabler;
		HttpContext.Current.Items["VersionCountDisabler"] = null;

		disabler?.Dispose();
	}
}

 
…and reference them in a config file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="Sitecore.Foundation.SitecoreProcessors.VersionCountDisablerBegin, Sitecore.Foundation.SitecoreProcessors"
                   patch:before="*[1]" />
      </httpRequestBegin>

      <httpRequestEnd>
        <processor type="Sitecore.Foundation.SitecoreProcessors.VersionCountDisablerEnd, Sitecore.Foundation.SitecoreProcessors" />
      </httpRequestEnd>
    </pipelines>
  </sitecore>
</configuration>

 

Finally, you will see that item is not null since it will fallback to the one you have set.

ItemNotNull

 
Hope this helps!
Happy Sitecoring 😉
 

 

 

 

5 thoughts on “Sitecore 8.2 Language Fallback – Glass Mapper

  1. If you are applying Helix principles to your solution then they should go under any foundation project where you extend Sitecore pipelines, processors etc., in my case Sitecore.Foundation.SitecoreProcessors project.
    If you are using any other structure you can place under a SitecoreProcessors class folder and you probably need to update the namespace of the classes and configs.

    Like

  2. We are not using helix, our old site uses 7.2 web forms. “SitecoreProcessors class folder” please excuse my ignorance, but where do I find this in directory structure? Would that be in the website\sitecore folder?

    Like

  3. That folder was an example. In your VS solution, you can create any folder, you can call it CustomProcessors, inside that folder you can add these two processors VersionCountDisablerBegin and VersionCountDisablerEnd (make sure you have the correct namespace, let’s say it would be YOURPROJECT.CustomProcessors) and you need to add a new config file (I guess you have a App_Config/Include folder where you patch some Sitecore settings), and add these settings: “YOURPROJECT.CustomProcessors.VersionCountDisablerBegin, YOURPROJECT” and “YOURPROJECT.CustomProcessors.VersionCountDisablerEnd, YOURPROJECT”

    Like

  4. Thanks again Santiago. Part of my issue is that I don’t have a solution for our website, I was handed the compiled website and asked to get it working in a new sitecore 8.2 instance. From looking at the files, the version of glass mapper the site is using is quite old, v2 when I look at the file details in explorer. So is it possible to do this without having access to a solution? Can I just create a patch file in the app_config/include folder and then somehow add the class to another folder and viola, it will work?

    Thanks for all of your help.

    Like

Leave a comment