Can I use relative fast queries in the SitecoreQuery attribute?
Asked Answered
B

1

5

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.

Bursa answered 24/1, 2014 at 11:13 Comment(0)
P
7

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.

Pedigo answered 25/1, 2014 at 22:24 Comment(4)
I'm still getting the error "End of string expected at position 32" - I can't seem to find out what {path} is being replaced with to debug this further. I also copy/pasted what you wrote just to make sure. Thanks so far!Bursa
I just copy and pasted it again and it worked fine for me. Can you paste your code into here again?Pedigo
Could it be the path has items with keyword names in it, which Glass isn't handling? i.e 'and'. I know that database.SelectItems(query) will struggle with paths unless they've been escaped in the query.Cammi
I had the same problem and found that I can use {escapedPath} instead of {path}Ache

© 2022 - 2024 — McMap. All rights reserved.