Parent node using jcr.node

Avatar

Avatar

aravindS

Avatar

aravindS

aravindS

16-10-2019

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,

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

16-10-2019

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()

Answers (4)

Answers (4)

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

23-10-2019

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

Avatar

Avatar

aravindS

Avatar

aravindS

aravindS

16-10-2019

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.

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

16-10-2019

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

Avatar

Avatar

Ankur_Khare

MVP

Avatar

Ankur_Khare

MVP

Ankur_Khare
MVP

16-10-2019