Shivani Garg
Shivani Garg

Reputation: 735

Fetch all the values of specific property in aem using queryBuilder

I am having scenario in which i want to fetch all the values of a property under a specific path in AEM using QueryBuilder api. This property can have single or multivalued. Any help will be appreciated!!

Upvotes: 1

Views: 3584

Answers (2)

Utkarsh
Utkarsh

Reputation: 589

There is no direct way to fetch the properties using query builder api. I would suggest you to create a servlet resource, which requires a path and property name.

Fetch the jcr node using the given path via QueryBuilder. Then, you need to loop through the results to check the property of the nodes. You can access the multiple property values, once you have a node.

Upvotes: 2

VAr
VAr

Reputation: 2601

Example that can help you. is below as it is just for illustration written in simple JSP scriptlets

 <%
Iterator<Resource> iter = resourceResolver.findResources("/jcr:root/content/geometrixx-outdoors//element(*, nt:unstructured)[(@imageRotate = '0' or @imageRotate = '1')]","xpath");
while (iter.hasNext()) {
    Resource child = iter.next();
    out.println("</br>"+child.getPath());
    Node node = child.adaptTo(Node.class);
    Property nProp = node.getProperty("imageRotate");

if(nProp.isMultiple()) // This condition checks for properties whose type is String[](String array)  
      {  
Value[] values = nProp.getValues();
    out.println(" :: This is a multi valued property ::");
    for (Value v : values) {
        out.println("</br>"+"Property Name = "+nProp.getName()+" ; Property Value= "+v.getString());
    }  
      }  
      else if(!nProp.getDefinition().isMultiple()){  
          out.println("</br>"+"Property Name = "+nProp.getName()+" ; Property Value= "+nProp.getString());
      }  
}
%>

Here i have used the Iterator<Resource> iter = resourceResolver.findResources(query,"xpath"); which can give you the query results which matches the imageRotate property under /content/geometrixx-outdoors/ path which consists combination of single and multivalued as shown in below screenshot.

CRX Node

Upvotes: 4

Related Questions