Featured

Wednesday 16 December 2015

How to use querybuilder in cq5

It is important to know how to build a query in normal sql and need to implement in cq5 query form.
     We can have query types here , XPATH and JCR_SQL2.
     Can build a query using any of 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();
        }
    }
%>


Thanks,
Sony

No comments: