0

Can anyone help?

I have a path of a root page and I want to retrieve all the child pages of root page and grandchild and grandchild of root page. My structure is like this

rootPage
    |
    |
    |---------childPage
                    |
                    |
                    |---------grandChildPage
Rajesh Pandya
  • 1,540
  • 4
  • 18
  • 31
kokki
  • 1
  • 1
  • Does this answer your question? [AEM Get child node content](https://stackoverflow.com/questions/28663967/aem-get-child-node-content) – Leo Feb 18 '21 at 06:28

2 Answers2

0

I think this should work.


public static void visitRecursively(Node node, Session currentSession) {
  try{
    NodeIterator list = node.getNodes();

    while(list.hasNext())   {

      Node childNode = list.nextNode();
      // Verify child node for cqPage type
      if((childNode.hasProperty("jcr:primaryType")) && (childNode.getProperty("jcr:primaryType").getValue().getString()).equals("cq:Page") ){
                        
        Node jcrNode = childNode.getNode("jcr:content");
 
        // Iterate some of the page properties
        String articleTitle="";String jcrDesc="";String jcrTitle="";String keywords="";
        if(jcrNode.hasProperty("articleTitle")){

            articleTitle = jcrNode.getProperty("articleTitle").getString();
            log.info("articleTitle--->"+articleTitle);
        }
        if(jcrNode.hasProperty("jcr:description")){
    
            jcrDesc = jcrNode.getProperty("jcr:description").getString();
            log.info("jcr:description--->"+jcrDesc);
        }
        if(jcrNode.hasProperty("jcr:title")){
    
            jcrTitle = jcrNode.getProperty("jcr:title").getString();
            log.info("jcr:title--->"+jcrTitle);
        }
        if(jcrNode.hasProperty("keywords")){

            keywords = jcrNode.getProperty("keywords").getString();
            log.info("keywords--->"+keywords);
        }
 
        String pagePropertiesString = "articleTitle--->"+articleTitle + "jcr:description--->"+jcrDesc+"jcr:title--->"+jcrTitle + "keywords--->"+keywords ;

      log.info("Page Properties :---> Node "+ childNode.getName()+ "Properties : " + pagePropertiesString );
      }
      visitRecursively(childNode,currentSession);
    }
  } catch (RepositoryException rpe){
    log.info("Exception in recursive listing:");
  }
Roger
  • 156
  • 7
0

You can easily do this with SlingQuery

final List<Resource> descendantPages = $(rootPage).("cq:Page").asList();

source: https://sling.apache.org/documentation/bundles/sling-query.html

or with a plain SQL2 query

final String query = "SELECT page.* FROM [cq:PageContent] AS page "
    + "WHERE ISDESCENDANTNODE(page, '%s')";
resolver.findResources(String.format(query, rootPage.getPath()), JCR_SQL2);