Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cq5 get parent node properties

Tags:

jcr

aem

sling

I have component hierarchy parsys -> parentcomp -> childcomp

parentcomp node has some properties. I am trying to access parent parentcomp properties at childcomp level.

I am not sure how to do this.

Any idea how to get a parent node properties. I know following code, gives me the path having parent node as one of selector. But, not sure how to get to exactly specific node and read those properties.

<%= currentNode.getPath() %>

Thank you, Sri

like image 871
Sri Avatar asked Dec 28 '15 19:12

Sri


2 Answers

There are many ways to achieve this.

  1. Using the Resource API, where the resource object is available through inclusion of the global.jsp

    ValueMap parentProps = resource.getParent().getValueMap(); //in latest versions of AEM ValueMap parentProps = resource.getParent().adaptTo(ValueMap.class); //older versions

  2. Using the Node API using the currentNode object.

    PropertyIterator propertyIterator = currentNode.getParent().getProperties()

I would personally prefer using Resource API to achieve the same as it makes it a lot simpler to handle the properties.

like image 128
rakhi4110 Avatar answered Nov 20 '22 01:11

rakhi4110


currentNode is an instance of the Node interface from the JCR API.

A part of Node's contract is the getParent method that you can use to obtain a reference to a node's parent JCR Node.

If the content structure is as you describe it, currentNode.getParent().getProperties() will get you the properties of the parentcomp node. However, if what you're describing are AEM components, you're probably going to have some jcr:content nodes in between. Double-check the actual structure in CRXDE.

If you're only interested in a single, specific property, you can just use getProperty instead.

like image 36
toniedzwiedz Avatar answered Nov 19 '22 23:11

toniedzwiedz