Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

I want to get list of components present in currentpage by using a component

Avatar

Level 4

I want to get list of components present in currentpage by using a component and there is a flag like while checkbox is checked those components will be add as list , By using Backend it should be present on screen.

 

CurrentPage :   component 1
                          component 2

                          component 3

                          component 4

 

Any references please

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@keshava219 For this you can get list by following the below steps:

1. Create the sling model for the list component and get the currentPage using the annotation 

@ScriptVariable

private Page currentPage;
 
2. As the component could be present in any of the hierarchy, so better to find all the components using the query builder using the predicate map. For this you can do by finding all the paths having the property sling:resourceType with combination of the another property which has the cheecked value as true.
Map<String, String> map = new HashMap<String, String>();
map.put("path", <page-path>/jcr:content);
map.put("1_property", "sling:resourceType");
map.put("1_property.operation", "exists");
map.put("2_property", "sling:resourceType");
map.put("2_property.operation", "unequals");
map.put("2_property.value", "<project-name>/components/container");
map.put("3_property", "componentChecked");
map.put("3_property.value", "true");
map.put("group.p.and", "true");
map.put("p.limit", "-1");
    try {
            Session session = resourceResolver.adaptTo(Session.class);
            Query query = builder.createQuery(PredicateGroup.create(map), session);
            if (query != null) {
              SearchResult result = query.getResult();
              for (Hit hit : result.getHits()) {
                    .............
              }
            }
        } catch (IOException | RepositoryException  e) {
  logger.error("Error while processing data {}",e.getMessage());
}
 
Finally you will get the list of all the components on the page which has the property checked.
 
Hope this helps!
Thanks

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@keshava219 For this you can get list by following the below steps:

1. Create the sling model for the list component and get the currentPage using the annotation 

@ScriptVariable

private Page currentPage;
 
2. As the component could be present in any of the hierarchy, so better to find all the components using the query builder using the predicate map. For this you can do by finding all the paths having the property sling:resourceType with combination of the another property which has the cheecked value as true.
Map<String, String> map = new HashMap<String, String>();
map.put("path", <page-path>/jcr:content);
map.put("1_property", "sling:resourceType");
map.put("1_property.operation", "exists");
map.put("2_property", "sling:resourceType");
map.put("2_property.operation", "unequals");
map.put("2_property.value", "<project-name>/components/container");
map.put("3_property", "componentChecked");
map.put("3_property.value", "true");
map.put("group.p.and", "true");
map.put("p.limit", "-1");
    try {
            Session session = resourceResolver.adaptTo(Session.class);
            Query query = builder.createQuery(PredicateGroup.create(map), session);
            if (query != null) {
              SearchResult result = query.getResult();
              for (Hit hit : result.getHits()) {
                    .............
              }
            }
        } catch (IOException | RepositoryException  e) {
  logger.error("Error while processing data {}",e.getMessage());
}
 
Finally you will get the list of all the components on the page which has the property checked.
 
Hope this helps!
Thanks

Avatar

Level 4

@Shailesh_Bassi_  one thing

I'm not able to get same tree structure in page component,  Im getting different ordering,

path=mypage/tiles-list-tests/jcr:content
1_property=sling:resourceType
1_property.operation=exists
2_property=addToLocal
2_property.value=true

orderby=path
group.p.and=true
p.limit=-1

Avatar

Community Advisor

@keshava219

Your query can be simplified. You mentioned having a flag in each component node, and if that flag is present, you want to fetch it. You can also check the possible value of the flag. Your query can be as straightforward as follows:

 

path=/content/<your-path>
1_property=<propertyName>
1_property.operation=exists
2_property=<propertyName>
2_property.value=<value>
orderby=@jcr:path
p.limit=-1

 

To order your query by path, use `@jcr:path` (ref: This response ). You don't need to specify `group.p.and=true` since, by default, all groups are AND-ed between them.

 

Hope this helps. You can use this query in your backend using Querybuilder APIs, as mentioned above, and it should give you the required results.

Thanks

Veena ✌

Avatar

Level 4

HI @VeenaVikraman , thanks for the reply tried with below snippet in query
still the same thing like

             component 4
            component 2
            component 5

           component 3
           component 1

its giving me like the above order and Im expecting based on page  structure.

            component 1
            component 2
            component 3

           component 4
           component 5

might be it was reading with the sling:resourceType? 

 

Map<String, String> map = new HashMap<String, String>();
map.put("path", currentPage.getPath() + "/jcr:content");
map.put("1_property", "sling:resourceType");
map.put("1_property.operation", "exists");
map.put("2_property", "addToLocal");
map.put("2_property.value", "true");
map.put("orderby", "@jcr:path");
map.put("p.limit", "-1");