Getting child pages on the root page | Community
Skip to main content
February 29, 2024
Solved

Getting child pages on the root page

  • February 29, 2024
  • 4 replies
  • 935 views

A developer is creating a custom component using sling model on the page and listing all the child pages of the root page. Wants to show all child pages on the root page itself .How can we achieve that?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Imran__Khan

@nathes There are two ways:

1. As part of component dialog provide a path browser property to choose root path and read all its child in sling model.

2. Consider the current page as root page where we are dropping this component. Using current page you can iterate its child, get its value and return to Sightly to show on the page.

 

import com.day.cq.wcm.api.Page; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.Iterator; @Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class RootPathModel { @Inject private Page currentPage; @PostConstruct protected void init() { Iterator<Page> childPages = currentPage.listChildren(); while (childPages.hasNext()) { Page childPage = childPages.next(); // Collect data, store it in list and return to sightly. } } }

 

4 replies

Imran__Khan
Community Advisor
Imran__KhanCommunity AdvisorAccepted solution
Community Advisor
February 29, 2024

@nathes There are two ways:

1. As part of component dialog provide a path browser property to choose root path and read all its child in sling model.

2. Consider the current page as root page where we are dropping this component. Using current page you can iterate its child, get its value and return to Sightly to show on the page.

 

import com.day.cq.wcm.api.Page; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.Iterator; @Model(adaptables = { SlingHttpServletRequest.class, Resource.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class RootPathModel { @Inject private Page currentPage; @PostConstruct protected void init() { Iterator<Page> childPages = currentPage.listChildren(); while (childPages.hasNext()) { Page childPage = childPages.next(); // Collect data, store it in list and return to sightly. } } }

 

Shiv_Prakash_Patel
Community Advisor
Community Advisor
February 29, 2024
pulkitvashisth
Community Advisor
Community Advisor
February 29, 2024

@nathes 


Best practice would be to use the ootb List core component

 

https://experienceleague.adobe.com/docs/experience-manager-core-components/using/wcm-components/v2-components/list.html?lang=en

 

Its simple and ready to use. You can override scripts you want accordingly by proxying the component in your project using sling:resourceSuperType.

kautuk_sahni
Community Manager
Community Manager
March 7, 2024

@nathes Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni