List of components | Community
Skip to main content
Level 2
March 8, 2023
Solved

List of components

  • March 8, 2023
  • 2 replies
  • 1241 views

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.
Best answer by arunpatidar

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  

2 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
March 8, 2023

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 

 

 

 

ssaiAuthor
Level 2
March 8, 2023

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

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
March 8, 2023

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
Siva_Sogalapalli
Community Advisor
Community Advisor
March 8, 2023

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.