I want to get list of components present in currentpage by using a component | Community
Skip to main content
keshava219
Level 3
December 19, 2023
Solved

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

  • December 19, 2023
  • 2 replies
  • 1128 views

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

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 ShaileshBassi

@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

2 replies

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
December 19, 2023

@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
keshava219
Level 3
December 20, 2023

@shaileshbassi  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

VeenaVikraman
Community Advisor
Community Advisor
December 20, 2023

@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 ✌

keshava219
Level 3
December 21, 2023

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");