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

Getting grand parent node path from a child

Avatar

Level 7

Hi team,

 

I have a node structure like below where I need to get the Parent node path from the child node that meant I need get the grand parent path from a grand child. Like getting the current node path using ${currentNode.ideentifier} , Can be done using HTL or sling models. Please guide me in both ways

JakeCham_0-1685529522599.png

 

Thanks.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @JakeCham 

 

First you can inject a Current Resource in your child component sling model as below

 

@SlingObject
private Resource currentResource;
 
Then you can use above object to access grandparent valueMap properties. for example, in your case It will be 

 

String[] arr = currentResource.getParent().getParent().getValueMap();

It will return grandParent node properties valuemap in array.

Please post here if you face any issue.

View solution in original post

11 Replies

Avatar

Community Advisor

@JakeCham 

It's better to write all your business logic in sling models/BE services instead of Sightly. 

 

You can use below method to get appropriate parent page details in sling models or services. 

 

Page getParent(int level)
Returns the relative parent page. If no page exists at that level, null is returned. Example (this path == /content/geometrixx/en/products)
 | level | returned                        |
 |     0 | /content/geometrixx/en/products |
 |     1 | /content/geometrixx/en          |
 |     2 | /content/geometrixx             |
 |     3 | /content                        |
 |     4 | null                            |
 

Parameters: level - hierarchy level of the parent page to retrieve Returns: the respective parent page or null Additional info: https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Pa... https://www.tabnine.com/code/java/methods/com.day.cq.wcm.api.Page/getParent

Avatar

Level 7

Hi @Siva_Sogalapalli 

Thanks for the reply.

 

You suggested about currentPage.getParent(); This is not going to help me cas here I'm using a model class and that model class is calling from the grand child and I want to get the grand parent from grand child.

Avatar

Community Advisor

we can call getParent() on any page object and not necessarily on current page only.

so In your case, Get the page object using child page path/resource and then call getParent() method.

You have multiple options to convert it page object based on your child page path or object type.

 

For example:  If you just have child page path as string obj then you can try something like below.

public Page getPage(ResourceResolver resolver, String path) {
Resource pageResource = resolver.getResource(path);
if(pageResource!=null){
return pageResource.adaptTo(Page.class);
}
return null;
}

OR If you have child page info of type resource Object, you can simply adapt to Page.class.

 

Hope this helps. 

Avatar

Level 7

Hi, 

 

Im putting a child component inside parent component where in jcr there is a parsys also so it become grand relationship instead parent child.

I'm calling my model class inside child component where I need to access the parent node properties

Avatar

Community Advisor

Hi @JakeCham 

Can you please tell about the type of node?
Is child node a page, where you will drop a component and want to access grand parent page and read it's properties?

Avatar

Level 7

No Im putting a child component inside parent component where in jcr there is a parsys also so it become grand relationship instead parent child.

I'm calling my model class inside child component where I need to access the parent node properties 

Avatar

Correct answer by
Community Advisor

Hi @JakeCham 

 

First you can inject a Current Resource in your child component sling model as below

 

@SlingObject
private Resource currentResource;
 
Then you can use above object to access grandparent valueMap properties. for example, in your case It will be 

 

String[] arr = currentResource.getParent().getParent().getValueMap();

It will return grandParent node properties valuemap in array.

Please post here if you face any issue.

Avatar

Community Advisor

Check the page API, if you are looking to get the grandparent page, I think that you could use

 

page.getAbsoluteParent(int)

 

OR as @Siva_Sogalapalli  mentioned use 

 

page.getParent(int)

 

Make sure you pass a parameter "level" (level - hierarchy level of the parent page to retrieve), please read carefully the documentation about how to use those methods.


https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Pa...



Esteban Bustamante

Avatar

Level 7

Hi,

Im putting a child component inside parent component where in jcr there is a parsys also so it become grand relationship instead parent child.

I'm calling my model class inside child component where I need to access the parent node properties.

Avatar

Community Advisor

To get the grand parent node from the child node, you can use the Resource Util Interface.

This has a method getParent() which can be used as shown below -

Rohan_Garg_0-1685590959207.png

Reference URL - https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/index.html?org/apache...