Expand my Community achievements bar.

SOLVED

How to get the list of child pages?

Avatar

Level 2

How does this component get the list of child pages?

A developer is creating a custom component on the page /latestBlogs.html that needs to list all the titles of the blogs pages under /content/blogs.

How does this component get the list of child pages?


A. Instantiate a node object with session.getNode(/content/blogs) and then iterate through the child nodes and print the title for each.
B. Use PageManager.getPage(“/content/blogs”) of the static PageManager class to instantiate a Page object and then iterate through the child pages and print the title for each.
C. Use the QueryDebugger to look for all children of /content/blogs and then iterate through the result set and print the title for each.
D. Adapt the resourceResolver to the PageManager service, then use the getPage(/content/blogs) to instantiate a Page object and then iterate through the child pages and print the title for each.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @cmr96960454 

 

Each option has its pros and cons:

 

A. Instantiate a node object with session.getNode(/content/blogs) and then iterate through the child nodes and print the title for each.
I would not recommend it as in this case you will have to iterate each and every node including structure/component nodes under page. Basically you will have to apply your own check to identify if it is a page node.
B. Use PageManager.getPage(“/content/blogs”) of the static PageManager class to instantiate a Page object and then iterate through the child pages and print the title for each.

I do not understand what you mean by static PageManager
C. Use the QueryDebugger to look for all children of /content/blogs and then iterate through the result set and print the title for each.

This option is good and you can apply conditions to get specific blog pages conditionally on properties.
For large number of pages, this is better option as you can optimize the performance as well with oak:index.
D. Adapt the resourceResolver to the PageManager service, then use the getPage(/content/blogs) to instantiate a Page object and then iterate through the child pages and print the title for each.
This seems to be good option too and as seems more appropriate if your usecase is just iteration and displaying of blogs. But in future if number of pages increases to 100K or so, iteration can take up a lot of time. Also, you have to filter blog pages such as by date, Go for option C.

 

Thanks,

Nupur

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hi @cmr96960454 

 

Each option has its pros and cons:

 

A. Instantiate a node object with session.getNode(/content/blogs) and then iterate through the child nodes and print the title for each.
I would not recommend it as in this case you will have to iterate each and every node including structure/component nodes under page. Basically you will have to apply your own check to identify if it is a page node.
B. Use PageManager.getPage(“/content/blogs”) of the static PageManager class to instantiate a Page object and then iterate through the child pages and print the title for each.

I do not understand what you mean by static PageManager
C. Use the QueryDebugger to look for all children of /content/blogs and then iterate through the result set and print the title for each.

This option is good and you can apply conditions to get specific blog pages conditionally on properties.
For large number of pages, this is better option as you can optimize the performance as well with oak:index.
D. Adapt the resourceResolver to the PageManager service, then use the getPage(/content/blogs) to instantiate a Page object and then iterate through the child pages and print the title for each.
This seems to be good option too and as seems more appropriate if your usecase is just iteration and displaying of blogs. But in future if number of pages increases to 100K or so, iteration can take up a lot of time. Also, you have to filter blog pages such as by date, Go for option C.

 

Thanks,

Nupur

Avatar

Community Advisor

@cmr96960454 @Nupur_Jain 

 

My two cents on this one.

 

QueryDebugger should be used to perform dry runs  to see how your JCR queries are performing and optimize them accordingly.

QueryDebugger should not be your primary way of implementing the given business logic.

Also, session.getNode will require a lot more house keeping without any added benefits. you will get the required output but not without putting extra effort in terms of filtering page nodes from other node types

 

So , i think the correct answer is

D -  Adapt the resourceResolver to the PageManager service, then use the getPage(/content/blogs) to instantiate a Page object and then iterate through the child pages and print the title for each.

 

This way, any ACL restrictions on the content tree are also honored.