The many ways to retrieve children using Glass.Mapper.Sc

Glass.Mapper.Sc offers many different ways to get children and ways to filter those children, in this blog post we explore a few of these solutions.


First lets assume we have the following tree made up of a Home item with Product and News items beneath it, you shouldn't do this normally in Sitecore but it will work for out demonstration:


The simplest method to retrieve the child items is to use the children attribute:

    public class Home
    {
        public virtual IEnumerable<Child> Children { get; set; }
    }

This is great for getting all our Children as one type but it doesn't allow us to distinguish easily between News and Product items. To solve this we can use inferred types. I have created the following classes that represent my Product and News items, they both inherit from Child:

    [SitecoreType(TemplateId = "3706F23F-41F2-43B4-B2AC-E4C5D4F02B79")]
    public class Product : Child { }

    [SitecoreType(TemplateId = "888E2541-FC03-46B9-87BB-3D8B62CE2872")]
    public class News : Child { }

We can now update the home model to infer the type for each child item using the InferType property:

    public class Home
    {
        [SitecoreChildren(InferType = true)]
        public virtual IEnumerable<Child> Children { get; set; }
    }

This will now return a list containing 3 Product classes and 3 News classes, any child item that aren't either a Product or News item will be returned as the Child type.

So how do we get a list of child items that are just Products or just News? We can do this by using the SitecoreQuery attribute and using the IsRelative parameter:

    public class Home
    {    
        [SitecoreQuery("./*[@@templateid='3706F23F-41F2-43B4-B2AC-E4C5D4F02B79']", IsRelative = true)]
        public virtual IEnumerable<Product> Products { get; set; }

        [SitecoreQuery("./*[@@templateid='888E2541-FC03-46B9-87BB-3D8B62CE2872']", IsRelative = true)]
        public virtual IEnumerable<News> News { get; set; }
    }

We now have a nice way to get just the items we are interested in.

Finally we have one other options which is to use the EnforceTemplate property on the SitecoreType attribute, if we update the Product and News class to use this property:

    [SitecoreType(TemplateId = "3706F23F-41F2-43B4-B2AC-E4C5D4F02B79", EnforceTemplate = SitecoreEnforceTemplate.Template)]
    public class Product : Child { }

    [SitecoreType(TemplateId = "888E2541-FC03-46B9-87BB-3D8B62CE2872", EnforceTemplate = SitecoreEnforceTemplate.Template)]
    public class News : Child { }

We need to update the home model too, notice that we are now using the SitecoreChildren attribute for both properties:

    public class Home
    {
        [SitecoreChildren]
        public virtual IEnumerable<Product> Products { get; set; }

        [SitecoreChildren]
        public virtual IEnumerable<News> News { get; set; }
    }

With the EnforceTemplate property set Glass.Mapper.Sc will check to see if the item being mapped matches the ID of the template defined by the SitecoreType attribute. If it does then it returns the mapped item otherwise it skips it. This then quickly filters the list.

That concludes this quick blog post on the many different ways to get and filter child items using Glass.Mapper.Sc. If you want to learn more about how to use Glass.Mapper.Sc then why not join one of our training courses.

comments powered by Disqus