Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 5

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. 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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");
    }
}

View solution in original post

8 Replies

Avatar

Community Advisor

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

 

Avatar

Level 5

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_  

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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");
    }
}

Avatar

Level 1

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

page.getParent().listChildren();

Avatar

Community Advisor

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

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