Get a node form currenPage in CQ5

Riju Mahna picture Riju Mahna · Dec 16, 2013 · Viewed 19.6k times · Source

I have a component that needs to fetch properties from another component on the same page.

Is there a way to get a component's NODE object from currentPage ?

I have the name of the node I need to fetch available in the code.

Answer

santiagozky picture santiagozky · Dec 16, 2013

Assuming the node you need is in Page/jcr:content/yournode:

the Page object has a method getContentResource() which returns you the jcr:content resource node by default. You can also use page.getContentResource("yournode") to get a specific node below jcr:content.

If your node, for some reason is sibling to jcr:content (it shouldn't btw), you can your iterate the children of a resource using resource.listChildren().

Remember, this is all Sling API, so you are managing resources, not nodes. You can get a JCR Node from a resource using resource.adaptTo(Node.class)