I am trying to create an an extension method to get all children of a derived type. This would be an extension method on my base glass item. But it returns null
. Here is my method:
I register my types for preload like this:
var attributes = new AttributeConfigurationLoader("DD.Domain");
return new IConfigurationLoader[]{ attributes };
My base glass item has a Children
property like this:
[SitecoreType(AutoMap = true)]
public partial interface IBaseGlassItem : ISitecoreItem
{
[SitecoreChildren(InferType = true)]
IEnumerable<IBaseGlassItem> Children { get; }
}
public partial interface ISitecoreItem
{
[SitecoreId]
ID ID { get; set; }
[SitecoreInfo(SitecoreInfoType.TemplateId)]
ID TemplateID { get; set; }
}
Then I try to get some items:
public static IEnumerable<TChild> GetChildrenDerivedFrom<TChild>(this IBaseGlassItem item, ID templateId)
where TChild : class, IBaseGlassItem
{
var children = item.Children.Where(c => c.IsDerived(templateId)).OfType<TChild>();
return children;
}
And here is my type that I am actually trying to get:
[SitecoreType(AutoMap = true, TemplateId = PromotionOrEventDetailsFieldsConstants.TemplateIdString)]
public interface IPromotionOrEventDetailFields : IBaseGlassItem
{
[SitecoreField]
Image Image { get; set; }
[SitecoreField]
string HoverText { get; set; }
[SitecoreField]
string ShortTitle { get; set; }
[SitecoreField]
string Caption { get; set; }
[SitecoreField]
Item ColorTheme { get; set; }
}
But this never works because the .OfType<Tchild>
ends up returning null
because it thinks the children are of type IBaseGlassItem
.
I suspect that maybe my types are not registered but how can I confirm this?
UPDATE
I figured out how to view what Glass Mapper has registered, and that is not the problem. My item is registered.
.Where(c => c.IsDerived(templateId))
. TheOfType<TChild>
call should already filter down the children to just the template you're interested in, thanks to InferType already using the most-derived type for each model.