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
|
|
|
|----------------------grandGrandChildPages
So I want the path of all these pages. How can I get this? currently, I'm using this
adminSession = repository.loginAdministrative( repository.getDefaultWorkspace());
ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
List<String>pageList = new ArrayList();
Page rootPage = pageManager.getPage("/content/abc/roi");
Iterator<Page> rootPageIterator = rootPage.listChildren();
while(rootPageIterator.hasNext())
{
Page childPage = rootPageIterator.next();
String path = childPage.getPath();
pageList.add(path);
}
But it provides me the only the child pages of root page and if I use rootPage.hasChild() then I have to pass the string which I can't because I don't know the name of pages. Is there any method which returns boolean value whether a page has child pages or not. I just want a list of all pages inside the root page which include grandChild as well as grandgrandCHild of root page.
can anyone help?
Best How To:
From AEM 5.6 onwards, you can use the listChildren(Filter<Page> filter, boolean deep) method available in com.day.cq.wcm.api.Page interface.
This would accept a PageFilter and a boolean value as arguments.
The PageFilter can be used, if you want to filter invalid and hidden pages. If you want all the pages to be listed pass null for this param.
For the boolean deep, if false it lists only the child pages, and if true would list all the descendants of the given page.
Hence, the code in the question can be modified either toIterator<Page> rootPageIterator = rootPage.listChildren(null, true);
or asIterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(), true);
No comments:
Post a Comment
If you have any doubts or questions, please let us know.