Skip to content

Instantly share code, notes, and snippets.

@Krusen
Created July 13, 2017 06:31
Show Gist options
  • Select an option

  • Save Krusen/159d9c02f41e8cee7d12f098adb4233f to your computer and use it in GitHub Desktop.

Select an option

Save Krusen/159d9c02f41e8cee7d12f098adb4233f to your computer and use it in GitHub Desktop.
Glass Mapper GetSharedItem<T> extension methods
using System;
using Glass.Mapper.Sc;
using Sitecore.Globalization;
namespace TranslatePlusModule.Extensions
{
public static class SitecoreServiceExtensions
{
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the current language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, string path) where T : class
{
using (new VersionCountDisabler())
{
return sitecoreService.GetItem<T>(path);
}
}
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the specified language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, string path, Language language) where T : class
{
using (new VersionCountDisabler())
{
return sitecoreService.GetItem<T>(path, language);
}
}
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the specified language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, string path, string languageName) where T : class
{
using (new VersionCountDisabler())
{
var language = Language.Parse(languageName);
return sitecoreService.GetItem<T>(path, language);
}
}
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the current language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, Guid guid) where T : class
{
using (new VersionCountDisabler())
{
return sitecoreService.GetItem<T>(guid);
}
}
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the specified language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, Guid guid, Language language) where T : class
{
using (new VersionCountDisabler())
{
return sitecoreService.GetItem<T>(guid, language);
}
}
/// <summary>
/// Returns an item with only the shared fields instead of null if the item does not have a version in the specified language.
/// </summary>
public static T GetSharedItem<T>(this ISitecoreService sitecoreService, Guid guid, string languageName) where T : class
{
using (new VersionCountDisabler())
{
var language = Language.Parse(languageName);
return sitecoreService.GetItem<T>(guid, language);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment