Parent node using jcr.node | Community
Skip to main content
Level 5
Oct 16, 2019 7:27 AM UTC
Solved

Parent node using jcr.node

Hi,

How to get the parent node (first parent node with specific primary type ex: cq:Page) for the child node.

Ex:

/content/dam/page(cq:Page)/jcr:content/root/property

Node node = till property.

how to get the cq:page node from property node.
I used node.getParent(); ---  that place might vary.

Thanks in Advance,

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Ankur_Khare
Ankur_KhareAccepted solution
New Participant
Oct 16, 2019 9:48 AM UTC

Get first ancestor with a given template

$(resource).closest("cq:Page[jcr:content/cq:template=/apps/geometrixx/templates/homepage]") 

List siblings of the current page which can be displayed in the navigation

$(resource).closest("cq:Page").siblings("cq:Page[jcr:content/hiddenInNav=false]") 

Get the first sibling of the current page

$(resource).closest("cq:Page").siblings("cq:Page").first() 

Get page ancestor closest to the root

$(resource).parents("cq:Page").last()

5 replies

Ankur_Khare
New Participant
Oct 23, 2019 11:30 AM UTC

is it resolved?if yes pls mark it as correct..

Translate
Ankur_Khare
Ankur_KhareAccepted solution
New Participant
Oct 16, 2019 9:48 AM UTC

Get first ancestor with a given template

$(resource).closest("cq:Page[jcr:content/cq:template=/apps/geometrixx/templates/homepage]") 

List siblings of the current page which can be displayed in the navigation

$(resource).closest("cq:Page").siblings("cq:Page[jcr:content/hiddenInNav=false]") 

Get the first sibling of the current page

$(resource).closest("cq:Page").siblings("cq:Page").first() 

Get page ancestor closest to the root

$(resource).parents("cq:Page").last()
Translate
aravindSAuthor
Level 5
Oct 16, 2019 9:16 AM UTC

Thanks for you comment,

using iteration we able to get the parent node. Is there any other way to get the parent node (specific primaryType) using SQL2 or any other function.

Translate
Ankur_Khare
New Participant
Oct 16, 2019 8:11 AM UTC

The recommended way to find resources in the Sling repository is using tree-traversal methods, like listChildren() and getParent() rather than JCR queries. The latter are great for listing resources with given properties, but we can't leverage the repository tree structure with such queries. On the other hand, using tree-traversal method is quite verbose. Consider following code that takes an resource and returns its first ancestor, being cq:Page, with given jcr:content/cq:template attribute:

Resource resource = ...; while ((resource = resource.getParent()) != null) { if (!resource.isResourceType("cq:Page")) { continue; } Resource template = resource.getChild("jcr:content/cq:template"); if (template != null && "my/template".equals(template.adaptTo(String.class))) { break; } } if (resource != null) { // we've found appropriate ancestor } 

SlingQuery is a tool that helps creating such queries in a more concise way. Above code could be written as:

import static org.apache.sling.query.SlingQuery.$; // ... $(resource).closest("cq:Page[jcr:content/cq:template=my/template]") 

Dollar sign is a static method that takes the resource array and creates SlingQuery object. The closest() method returns the first ancestor matching the selector string passed as the argument.

SlingQuery is inspired by the jQuery framework. jQuery is the source of method names, selector string syntax and the dollar sign method used as a collection constructor.

For more details pls check-

https://sling.apache.org/documentation/bundles/sling-query.html

Translate
Ankur_Khare
New Participant
Oct 16, 2019 7:47 AM UTC