Expand my Community achievements bar.

SOLVED

List of components

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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  



Arun Patidar

View solution in original post

4 Replies

Avatar

Community Advisor

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

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/solved-find-all-the-compon... 

 

 

 

Avatar

Level 2

@Saravanan_Dharmaraj I don't want to have it in groovy script -  I want to implement in service.

Avatar

Correct answer by
Community Advisor

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  



Arun Patidar

Avatar

Community Advisor

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.