July 7, 2016

Glass.Mapper - Retreive items without version

Today I will give you a very small trick to retrieve an item with Glass.Mapper even if the item didn't have any version in the current language. This is be helpful when you have an item with only some shared fields. Of course, the non-shared fields retrieved will be empty if you didn't have a version in the selected language.

The trick is very simple: you just need to surround it by a VersionCountDisabler

Here is two examples:
using(new VersionCountDisabler())
{
  var model =  sitecoreService.GetItem("/sitecore/content/home");
}
using(new VersionCountDisabler())
{
  var model =  myItem.Cast(item, isLazy, inferType);
}
You can find it in the official documentation but this isn't a very well-known feature: http://www.glass.lu/Mapper/Sc/Documentation/VersionCountDisabler

In bonus, here is the way to know if you are in a VersionCountDisabler mode in your code. This is usefull if you wrap the code with a custom method as I do.

bool isInVersionCountDisabler = (Switcher.CurrentValue == VersionCountState.Disabled);

1 comment:

  1. Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this content.

    Dot Net Training in Chennai ECR

    ReplyDelete