Skip navigation

CQ5

Arya123
Currently Being Moderated

how to get the list of component?

Jun 8, 2012 2:20 AM

    How to get the list of component through java API in Adobe CQ5?

 
Replies
  • Currently Being Moderated
    Jun 12, 2012 6:53 PM   in reply to Arya123

    There are probably more than a few ways, but here is a little component .jsp code that uses the jcr query api to pull out all the components and list them on a page.  If I know CQ, there might be a way to ask for a list of components another way though, maybe through another API, or possibly a .json url, I might remember reading something about that, somewhere, lol, but this at least gets you the list in a somewhat simple way.

     

    Code Example:

     

    <%@include file="/apps/psul/components/global.jsp"%>
    <%@ page import="javax.jcr.*,
                       javax.jcr.query.*"
    %>
     
     
     
     
    <%
       // Login to create an anonymous session on the default workspace
       Session session = resourceResolver.adaptTo(Session.class);
     
     
           //Declare a query for all the components in the system
           String SQL = "select * from cq:Component";
     
     
    //side note: if you want just the components in your site area, then make the query more like below... 
    //  SQL = "select * from cq:Component where jcr:path like '/apps/yoursitename/%'";
     
     
           QueryManager qm = session.getWorkspace().getQueryManager();
           Query query = qm.createQuery(SQL, Query.SQL);
           QueryResult result = query.execute();
           NodeIterator nodes = result.getNodes();
     
     
           while (nodes.hasNext()) {
               Node node = (Node)nodes.next();
               
               %>
                    <p><%=node.getName() %> (<b><%=node.getPath() %></b>)</p>
    <%     } %>
     
     
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points