Wednesday, December 28, 2016

Say Hello to Glass!!!!

Wanted to share something cool and powerful.  Not sure if you all are already aware, but, pretty awesome, so wanted to share regardless.


Goal – “To get descendants under an item in the content tree that are of specific template ID “

Many straight forward ways to achieve this. Like loading a VM on controller or a new property by casting and doing bunch of queries.
We are close to a launch and typically at this time, more code would mean more bugs and additional effort to resolve those.  I wanted to avoid this as much as possible. 

So, I tried loading what I needed through Glass like below.   That is it, Just two lines of code and everything loaded as I wished for. J
No casting, no object massaging, nothing at all.   So cool and yet so powerful.  It could come in handy for you all when working with hierarchical data.

 [SitecoreQuery(".//*[@@templateid='{A5617868-1226-4C7F-89A7-C7355BD1532A}']", IsRelative = true)]
       public virtual IEnumerable<MyTemplate> itemsOfMyTemplateUnderCurrent { get; set; }

Glass documentation helped me get a clue, but, it was more about children only and also note for some reason the sample format given by Glass did not work in case of descendants atleast. 
Notice the {} around GUID of template, my second try and wohoo!!! everything loaded in the object as I needed and wished for. 

Helpful reference:



No comments :

Post a Comment