May 9, 2020
Estimated Post Reading Time ~

How to use Query Builder in AEM

It is important to know how to build a query in normal SQL and need to implement it in the cq5 query form.
     We can have query types here, XPATH, and JCR_SQL2.
     Can build a query using any of the above.

<%@ page session="false" %><%
%><%@ page import="javax.jcr.*,
                   javax.jcr.query.*,
                   java.util.*,
                   org.apache.sling.api.resource.*,
                   org.apache.sling.jcr.api.SlingRepository,
                   org.apache.sling.commons.json.io.JSONWriter" %><%
%><%@ include file="/libs/foundation/global.jsp" %>
<%

    SlingRepository repo = sling.getService(SlingRepository.class);
   
    Session session = null;
    try {
        session = repo.loginAdministrative(null);
        QueryManager qm = session.getWorkspace().getQueryManager();

     1)   // QueryResult result = qm.createQuery("//element(*, cq:PageContent)[@demo]", Query.XPATH).execute();
     2)   QueryResult result = qm.createQuery("SELECT * FROM [cq:PageContent]",            javax.jcr.query.Query.JCR_SQL2).execute();

        NodeIterator nodes = result.getNodes();

        while (nodes.hasNext()) {
            Node node = nodes.nextNode();
         %><%=node.getPath()%><br/><%//--Here you can get the nodes info which you suggested in query
                }
    } finally {
        if (session != null) {
            session.logout();
        }
    }
%>


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.