5
votes

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

2

2 Answers

7
votes

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.

4
votes

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.