Getting a child node with their property Name | Community
Skip to main content
Level 3
July 12, 2022
Solved

Getting a child node with their property Name

  • July 12, 2022
  • 3 replies
  • 783 views

I have a container then layout then config then column then list I want to get those list property but node name changing again and again How can I extract the List name and there properties values without using name.

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

Hi @rohankalra ,

Try this,

Resource childResource = resourceResolver.getResource(child.getPath());
Node childNode = childResource.adaptTo(Node.class);
Node jcrContent = childNode.getNode("jcr:content");
NodeIterator childrenNodes = jcrContent.getNodes();

while(childrenNodes.hasNext()){
    Node next = childrenNodes.nextNode();
    String resourceType = next.getProperty("sling:resourceType").getString();
    if(resourceType.equals("your/custom/resource/property/path")) {
properties = childResource.adaptTo(ValueMap.class);
String propertyOne = properties.get("propertyOne", (String) null);
} }

3 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
July 12, 2022

@rohankalra can you please share sample node structure how it's getting stored in crx ?

Sachin_Arora_
Community Advisor
Community Advisor
July 12, 2022

You can update your logic to use regex for checking the node name. For example it will always be in these format

list / list_NNNNNNNN_ / list_NNNNNNNN

Else you can go ahead with search based approach. You can write a query to fetch all list components based on sling:resourceType property.

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
July 13, 2022

Hi @rohankalra ,

Try this,

Resource childResource = resourceResolver.getResource(child.getPath());
Node childNode = childResource.adaptTo(Node.class);
Node jcrContent = childNode.getNode("jcr:content");
NodeIterator childrenNodes = jcrContent.getNodes();

while(childrenNodes.hasNext()){
    Node next = childrenNodes.nextNode();
    String resourceType = next.getProperty("sling:resourceType").getString();
    if(resourceType.equals("your/custom/resource/property/path")) {
properties = childResource.adaptTo(ValueMap.class);
String propertyOne = properties.get("propertyOne", (String) null);
} }
Santosh Sai