Skip to content

Instantly share code, notes, and snippets.

@cardinal252
Last active October 13, 2015 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardinal252/b1b0119745d3d8efd65b to your computer and use it in GitHub Desktop.
Save cardinal252/b1b0119745d3d8efd65b to your computer and use it in GitHub Desktop.
Using Glass Mapper to get out Children Of Type
using System;
using System.Collections.Generic;
using System.Linq;
using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Attributes;
using NUnit.Framework;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;
namespace Glass.Mapper.Sc.Integration
{
// I would not get children like this in production, it is an example of how you can manipulate items in sitecore and return them
// through Glass Mapper
public class OfTypeFixture
{
/// <summary>
/// This gist is to show the getting of children from sitecore and subsequently using them with glass using a type limiting generic
/// I have used children as an example, but it could be applied to any scenario you might choose to do - axes / whatever.
///
/// NOTE: Inferred types are the more correct way to do this, but its not always so simple
/// </summary>
[Test]
public void GetTypedChildren()
{
// Assign
// initialize glass
var context = Context.Create(Utilities.CreateStandardResolver());
var db = Factory.GetDatabase("master");
var service = new SitecoreService(db);
// Get the standard sitecore system node
Guid id = new Guid("{13D6D6C6-C50B-4BBD-B331-2B04F1A58F21}");
//Act
var result = service.GetItem<IIdentifiable>(id);
//Assert
Assert.IsNotNull(result);
Assert.AreEqual(id, result.Id);
Assert.AreEqual("en", result.Language);
GlassTypeManager manager = new GlassTypeManager(service);
Assert.AreEqual(10, manager.ChildrenOfType<INode>(result).Count());
Assert.AreEqual(1, manager.ChildrenOfType<IFolder>(result).Count());
}
// Represents a sitecore node
[SitecoreType(TemplateId = "{239F9CF4-E5A0-44E0-B342-0F32CD4C6D8B}")]
public interface INode
{
}
// Represents a sitecore folder
[SitecoreType(TemplateId = "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}")]
public interface IFolder
{
}
}
public interface IIdentifiable
{
Guid Id { get; set; }
string Language { get; set; }
}
public class GlassTypeManager
{
private readonly ISitecoreService sitecoreService;
public GlassTypeManager(ISitecoreService sitecoreService)
{
this.sitecoreService = sitecoreService;
}
public IEnumerable<T> ChildrenOfType<T>(IIdentifiable identifiable) where T : class
{
Guid templateId = GetTemplateIdFromConfiguration<T>();
return ChildrenOfType<T>(identifiable, templateId);
}
public IEnumerable<T> ChildrenOfType<T>(IIdentifiable identifiable, Guid templateId) where T : class
{
if (templateId == Guid.Empty)
{
throw new Exception("An invalid or empty template id was specified");
}
Item item = sitecoreService.Database.GetItem(new ID(identifiable.Id), Language.Parse(identifiable.Language));
if (item == null)
{
throw new Exception("The item could not be found");
}
var children = item.GetChildren();
if (children == null || children.Count == 0)
{
return Enumerable.Empty<T>();
}
ID templateIdToFind = new ID(templateId);
Item[] resultant = children.Where(x => x.TemplateID == templateIdToFind).ToArray();
return GetResults<T>(resultant);
}
private IEnumerable<T> GetResults<T>(Item[] resultant) where T : class
{
return resultant.Length > 0
? resultant.Select(x => sitecoreService.Cast<T>(x))
: Enumerable.Empty<T>();
}
private Guid GetTemplateIdFromConfiguration<T>() where T : class
{
Type type = typeof(T);
SitecoreTypeConfiguration sitecoreTypeConfiguration =
sitecoreService.GlassContext.GetTypeConfigurationFromType<SitecoreTypeConfiguration>(type);
if (sitecoreTypeConfiguration == null)
{
throw new Exception("Glass does not know about this type");
}
Guid templateId = sitecoreTypeConfiguration.TemplateId.ToGuid();
return templateId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment