2
votes

i am working on aem 6.3 and would like to get page name

SELECT * FROM [cq:Page] WHERE ISDESCENDANTNODE("/content/Product/Silhouettes/Accessories/Bands/Headband")

If I need to retrieve name of the nodes using sql-2 , how do I achieve it?

2
If you think any of the below answers has helped you solve the problem, could you please mark one of them as right, for others that might stumble over the same problem.SubSul

2 Answers

0
votes

You can specify column constraints like title, node name, etc this way -

SELECT nodeSet.name, nodeset.title FROM [cq:Page] AS nodeSet WHERE ISDESCENDANTNODE("/content/Product/Silhouettes/Accessories/Bands/Headband")

Note: the query tool in AEM(Tools -> Query) will not list the query results according to the columns you've mentioned, it will only list the node paths.

You can look at using /etc/importers/bulkeditor.html or AEM fiddle tool to visualize the query results based on column constraints.


If you want to achieve this programmatically, you can use the same query as you've mentioned in your question and use javax.jcr.query.* and javax.jcr.Node.* API's to retrieve just about any property from the query result. This article here should help you achieve this programatically.

0
votes

Use the ResourceResolver API to execute and obtain the query results:

final Iterator<Resource> pagesIterator = resolver.findResources('<your_query_here>', javax.jcr.query.Query.JCR_SQL2);
while (pagesIterator.hasNext()) {
    final Resource pageResource = pagesIterator.next();
    LOG.info(pageResource.getName());
}

However, please note that if you are using any version higher then CQ 5.6, you should use instead the Page API.

In this case, the listChildren(Filter<Page> filter, boolean deep) method will do the job.

The PageFilter parameter may be used if you want to filter through pages of some kind. So if no extra criteria for your page finding algorithm, your may pass null or a new empty object.

The boolean parameter: if false it returns only direct child pages, and if true would list all the descendant pages of the given page.

Therefore, the equivalent solution of the SQL Query which would provide you the same end results would be:

Iterator<Page> rootPageIterator = rootPage.listChildren(null, true);