Abstract
Sling Query is basically a Sling resource tree traversal tool created by Apache inspired by the jQuery JavaScript API. Sling Query is very effective when we need to traverse through content tree in AEM.
In this tutorial i will try to cover below topic:-
1. Introduction to Sling Query
2. When to Use Sling Query
3. Sling Query vs JCR Query
4. How to include Sling Query in AEM
5. Sling Query Examples
Introduction to Sling Query:-
As mentioned above AEM Sling Query is a resource traversal tool. In AEM for searching a content it is always recommended to go for traversal using listChildren() or getChildren() or Resource API instead of writing JCR Queries because performing query is always a costly operation as compared with traversal, depending upon the requirement. Sling Query is not an alternative for JCR Queries that we will see below when to use JCR queries and when we should use Sling Query.
When to Use Sling Query
We are clear now that when we need to traverse the content in AEM , first approach is to handle it with using methods like listChildren() and traverse using loops. But for example we need to traverse 10 level down and check get the first ancestor, writing normal tree traversal using Resource API is very cumbersome because it involves a lot of while()s, iterators and null checks and with time code complexity is also increasing, in such cases it is highly recommended to go for Sling Query, because it involves lazy evaluation of query result means it don’t query resources unless we need them. We will see it as part of one example below.
Read Full Blog
Q&A
Please use this thread to ask the related questions.
Kautuk Sahni