List of components | Community
Skip to main content
Level 2
Mar 8, 2023 2:01 AM UTC
Solved

List of components

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
arunpatidar
arunpatidarAccepted solution
New Participant
Mar 8, 2023 9:47 AM UTC

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

4 replies

Siva_Sogalapalli
New Participant
Mar 8, 2023 10:25 AM UTC

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.

Translate
Saravanan_Dharmaraj
New Participant
Mar 8, 2023 2:40 AM UTC

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-components-on-the-current-page-in-aem/td-p/577223 

 

 

 

Translate
ssaiAuthor
Level 2
Mar 8, 2023 2:41 AM UTC

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

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Mar 8, 2023 9:47 AM UTC

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  

Translate
Arun Patidar