Expand my Community achievements bar.

SOLVED

Returning list of nested child pages at given path in JSON format. after passing the page path

Avatar

Level 2

Code sample to get the list of nested child pages at a given path in json format using componentexporter

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@kalyan_chakrav1 

Check the below code snippet, hope this is what you are expecting.

@SlingObject
ResourceResolver resourceResolver;

@Self
SlingHttpServletRequest request;

private List<String> children = new ArrayList<String>();


private String root = "/content/test";

@PostConstruct
protected void initialize() {
    Page rootPage = resourceResolver.getResource(root).adaptTo(Page.class);
    Iterator<Page> listChildren = rootPage.listChildren(null, true);
    while(listChildren.hasNext()) {
	children.add(listChildren.next().getPath());
    }
}

public List<String> getChildren() {
	return children;
}

public String getRoot() {
	return root;
}

View solution in original post

5 Replies

Avatar

Community Advisor

@kalyan_chakrav1,

Can you please elaborate on your question? It is not making sense...

Avatar

Level 2
Using Component exporter i need to get all the child pages based on the homepage path

Avatar

Community Advisor

The component exporter's purpose is to output JSON from your sling model's properties via Apache Sling provides a Jackson JSON exporter. What you can do is write a recursive method that will traverse through each and every node, from the root node, and save each parent within a List<Page> object. Once your List<Page> is generated, you can simply create a getter for List<Page>, and calling model.json, you will get a JSON representation of all the child pages, recursively.

Take a look at ACS Common's sitemap generator, as they recusively traverse through every node to construct the sitemap.xml, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...

 

 

Avatar

Community Advisor

Please use Page APi in your model to get all the page and create json based on results 

https://helpx.adobe.com/experience-manager/6-2/sites/developing/using/reference-materials/javadoc/co...

 



Arun Patidar

Avatar

Correct answer by
Community Advisor

@kalyan_chakrav1 

Check the below code snippet, hope this is what you are expecting.

@SlingObject
ResourceResolver resourceResolver;

@Self
SlingHttpServletRequest request;

private List<String> children = new ArrayList<String>();


private String root = "/content/test";

@PostConstruct
protected void initialize() {
    Page rootPage = resourceResolver.getResource(root).adaptTo(Page.class);
    Iterator<Page> listChildren = rootPage.listChildren(null, true);
    while(listChildren.hasNext()) {
	children.add(listChildren.next().getPath());
    }
}

public List<String> getChildren() {
	return children;
}

public String getRoot() {
	return root;
}