Query AEM for CQ Pages

Friday, March 17th 2023

To query AEM for cq:Pages with a certain sling:resourceType and property value, you can use the QueryBuilder API provided by AEM. Here's an example Java code snippet that demonstrates how to build a query for this purpose:

import javax.jcr.Session; import javax.jcr.query.Query; import com.day.cq.search.PredicateGroup; import com.day.cq.search.QueryBuilder; import com.day.cq.search.QueryBuilderResult; import com.day.cq.search.SimplePredicate; import com.day.cq.search.result.Hit; import com.day.cq.search.result.SearchResult; Session session = // obtain JCR session QueryBuilder queryBuilder = session.adaptTo(QueryBuilder.class); String path = "/content/mysite"; // starting path for the search String resourceType = "myproject/components/mycomponent"; // resourceType to search for String propertyName = "myProperty"; // property name to search for String propertyValue = "myValue"; // property value to search for // Create predicates for resourceType and propertyValue SimplePredicate typePredicate = new SimplePredicate("sling:resourceType", resourceType); SimplePredicate valuePredicate = new SimplePredicate(propertyName, propertyValue); // Create a predicate group for the two predicates PredicateGroup predicateGroup = PredicateGroup.create(typePredicate, valuePredicate); // Create the query Query query = queryBuilder.createQuery(predicateGroup, session); // Set the starting path for the search query.setStartOrOffset(0); // Execute the query QueryBuilderResult result = query.getResult(); // Get the search results as hits SearchResult searchResult = result.getSearchResult(); for (Hit hit : searchResult.getHits()) { // Handle each hit as needed }

This code builds a query using the QueryBuilder API that searches for cq:Page nodes with a sling:resourceType property set to myproject/components/mycomponent and a myProperty property set to myValue. You can modify the resourceType, propertyName, and propertyValue variables to search for different property values.

Note that this code assumes that you have a JCR session object available for use in the query. You may need to obtain a session object using the appropriate API depending on your application's architecture.


Equivalent XPath Query

/jcr:root/content/mysite//element(*, cq:Page)[@sling:resourceType='myproject/components/mycomponent' and @myProperty='myValue']

This XPath query searches the JCR repository under the /content/mysite path for all cq:Page nodes that have a sling:resourceType property set to myproject/components/mycomponent and a myProperty property set to myValue.

You can modify the path and the property names and values as needed to suit your specific use case. Note that XPath queries are case-sensitive, so make sure to use the correct casing for property names and values.


Equivalent JCR SQL2 Query

SELECT * FROM [cq:Page] AS page WHERE ISDESCENDANTNODE('/content/mysite') AND page.[sling:resourceType] = 'myproject/components/mycomponent' AND page.[myProperty] = 'myValue'

This SQL2 query searches for all cq:Page nodes that have a sling:resourceType property set to myproject/components/mycomponent and a myProperty property set to myValue, under the /content/mysite path.

You can modify the FROM clause, the ISDESCENDANTNODE function, and the property names and values as needed to suit your specific use case.

Note that while SQL2 queries are more powerful than XPath queries, they can also be more complex to write and may require more knowledge of the JCR query language.