how to access the sibling pages of a page if I only have the Path of page? | Community
Skip to main content
Level 4
September 1, 2022
Solved

how to access the sibling pages of a page if I only have the Path of page?

  • September 1, 2022
  • 2 replies
  • 1554 views

I want to get sibling pages of a particular page  of which I only have url

 

I know how to get sibling pages of the current page, I used Page class to fetch the sibling pages but 

I want to know if I can do it for other pages if I have the URL.

Please help me to find out if it's possible and if so how. 

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 Sachin_Arora_

resourceResolver is coming null

@sachin_arora_ 


Sample code from default project structure. 

@Model(adaptables = Resource.class)
public class HelloWorldModel {

    @SlingObject
    private ResourceResolver resourceResolver;

    @PostConstruct
    protected void init() {
       Resource res =  resourceResolver.getResource("/content/we-retail");
    }
}

2 replies

Sachin_Arora_
Community Advisor
Community Advisor
September 1, 2022

If you are able to fetch siblings of current page, then same logic you can use for other pages as well as you already have a path. You can use ResourceResolver to get Page resource and use same logic.

resourceResolver.getResource(PAGE_PATH).adaptTo(Page.class);

To get siblings, you can just get Parent of a Page and then iterate child nodes as explained here
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/siblings/td-p/168892

 

Level 4
September 6, 2022

how to use resourceresolver, I am getting null pointer exception while using this

resourceResolver.getResource(PAGE_PATH).adaptTo(Page.class);

 

@ScriptVariable
private ResourceResolver resourceResolver;

I have used it as above and getting null point exception.

@sachin_arora_  

Sachin_Arora_
Community Advisor
Community Advisor
September 6, 2022

Please confirm if resourceResolver is coming null or resourceResolver.getResource(PAGE_PATH)?

September 1, 2022

Once you get the Page object you can use this to get all the siblings of that page.

page.getParent().listChildren();

Level 4
September 1, 2022

I don't have page object , I only have page path

Sachin_Arora_
Community Advisor
Community Advisor
September 2, 2022

As explained above, you can get Page object using this code.

resourceResolver.getResource(PAGE_PATH).adaptTo(Page.class);