Search code examples
aemjcrsling

cq5 get parent node properties


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


Solution

  • 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.