Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

kalyan_chakrav1
Level 2
Level 2

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

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
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

0 Replies
BrianKasingli
Community Advisor
Community Advisor

@kalyan_chakrav1,

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

kalyan_chakrav1
Level 2
Level 2
Using Component exporter i need to get all the child pages based on the homepage path
BrianKasingli
Community Advisor
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...

 

 

Anudeep_Garnepudi
Correct answer by
Community Advisor
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