Search code examples
c#sitecoreglass-mapper

Glass mapper populate list from Link path


I'm trying to populate a list on my current model with subitems from a different folder.

On my current model I want to define a field of type General Link in which I will select an item from Sitecore who has different subitems. The "public virtual IEnumerable List" should be populated with the above subitems.

I have read different posts related to how you can make the following type of queries:

[SitecoreQuery("./*[@@templatename='Testimonial']", IsRelative = true)]
public virtual IEnumerable Children { get; set; }

but this does not apply in my case because most probably I will have this template in different areas which I do not want to be included here.

Does anyone know if this is possible with Glass or should I just use a custom query to populate the list with an item subitems?


Solution

  • If you use a droplink or droptree field, rather than a General Link, you could do what you want by creating a generic Folder model.

    namespace MySite.Models
    {
        [SitecoreType(AutoMap = true)]
        public class Folder<T> : GlassBase
        {
            [SitecoreChildren]
            public virtual IEnumerable<T> Children { get; set; }
        }
    }
    

    And then use it from another model like so:

    [SitecoreField("My Link Field")]
    public virtual Folder<ChildModel> MyLinkField { get; set; }