Search code examples
c#sitecoresitecore7glass-mapper

Can I use relative fast queries in the SitecoreQuery attribute?


I have a working installation of Sitecore 7 + Glass Mapper 3 which I am looking to optimize.

An example of my code is:

[SitecoreQuery(".//*[@@templateid = '{011EC776-D9F3-4D73-8D8D-E454417E7574}']", 
                                                               IsRelative = true)]
IEnumerable<ItineraryLine> Itinerary { get; set; }

I was hoping to use FastQuery but I get the error:

End of string expected at position 4

I gave the following solution a try this involves slotting in to the getLookupSourceItems pipeline - but I don't think this is the right pipeline as it doesn't trigger in debug.

Is there another pipeline (if at all) that Glass uses in this scenario? Is there a different way I can solve my goal of speeding this up?

If I wasn't using attributes but extension methods I could do this manually and use *[@@id=''] to set the root node, but I was hoping to avoid this if possible.


Solution

  • When using the IsRelative setting to true GMS pushes the query throught Axes SelectItem. Sitecore does not allow fast query for Axes selects, e.g.:

    Item.Axes.SelectItems("fast:./*");
    

    See documentation here page 3:

    http://www.iosh.co.uk/~/media/using%20sitecore%20fast%20query001.ashx

    However GMS being awesome allows us to solve this another way, you can put placeholders in your query that GMS will expand. Removing the IsRelative property and using the {path} placeholder allows the same result:

    [SitecoreQuery("fast:{path}//*[@@templateid = '{011EC776-D9F3-4D73-8D8D-E454417E7574}']")]
    IEnumerable<ItineraryLine> Itinerary { get; set; }
    

    The path placeholder will be expanded to the full path of the current item being mapped.