Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 4

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
Level 9

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

Level 9

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 4

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.

@sachinarora  

Avatar

Level 9

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

Avatar

Correct answer by
Level 9

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

Level 9

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

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