cq5 get parent node properties
Asked Answered
T

2

5

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

Tetrode answered 28/12, 2015 at 19:34 Comment(0)
S
8

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.

Schriever answered 29/12, 2015 at 0:51 Comment(1)
I used resource approach to achieve this. ValueMap parentProps = resource.getParent().adaptTo(ValueMap.class);Tetrode
S
4

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.

Sheelah answered 29/12, 2015 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.