How to fetch list of components configured on current page?
I tried
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
Collection<com.day.cq.wcm.api.components.Component> components = componentManager.getComponents();
for (com.day.cq.wcm.api.components.Component component : components) {
if (component.getPath().startsWith("/apps")) {
}
}
but it 's not listing the exact components which has been configured on the page.
Any other way can someone answer may be with predicate query or something easier and efficient way?
Solved! Go to Solution.
Views
Replies
Total Likes
you can write same logic in service as well.
Groovy is a java scripting language.
1. Get the current page/jcr:content node
2. get all the children node (recursive) and collect the resources in a Set
code example https://aemlab.blogspot.com/2019/07/get-json-response-of-aem-page.html
You can get current page's jcr:content node and get all its children using getNodes() and check the sling:resourceType property of the components whose value starts like example "companyName" if you have components under /apps/companyName.
Some groovy script example discussed here
@Saravanan_Dharmaraj I don't want to have it in groovy script - I want to implement in service.
you can write same logic in service as well.
Groovy is a java scripting language.
1. Get the current page/jcr:content node
2. get all the children node (recursive) and collect the resources in a Set
code example https://aemlab.blogspot.com/2019/07/get-json-response-of-aem-page.html
Also, the other approach, you can get page json directly with below url:
http://localhost:4502/{pagepath}.{level}.json
Here level is the location where all the components presents.
Eg: http://localhost:4502/content/wknd/us/en/magazine/guide-la-skateparks.5.json once you have the json, you can parse the way you want.
Views
Likes
Replies