SOLVED: Find all the components on the current page in AEM? | Community
Skip to main content
New Participant
March 2, 2023
Solved

SOLVED: Find all the components on the current page in AEM?

  • March 2, 2023
  • 5 replies
  • 4747 views

Hi,

I need to find all components of a certain type on the current page in AEM?

I tried to do it using 

 

ComponentManager componentManager = request.adaptTo(ComponentManager.class);


Collection<Component> components = componentManager.getComponents();

but my componentManager is null? 

 

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 Nitin_laad

check this out - groovy script -

import com.day.cq.wcm.api.Page import com.day.cq.wcm.api.PageManager def pagePath = "/content/mysite/mypage" def pageManager = sling.getService(PageManager.class) def page = pageManager.getPage(pagePath) def componentType = "mysite/components/mycomponent" def components = page.contentResource?.children?.iterator() // Iterate over each component and check if it's of the specified type while (components?.hasNext()) { def component = components.next() if (component.resourceType == componentType) { println "Found component of type ${componentType} at path ${component.path}" } }

5 replies

anasusticAuthor
New Participant
March 8, 2023

This is how I solved it and it works:

 

Optional.ofNullable(request.getResourceResolver().getResource(currentPage.getPath())) .map(r -> ResourceModelUtil.getAllResourcesFromResourceType(r, Constants.ResourceTypes.YOUR_RESOURCE_TYPE)) .stream() .flatMap(Collection::stream) .continue_chaining_or_collect

 

New Participant
March 8, 2023

@anasustic is there any custom method in your ResourceModelUtil ?

 

anasusticAuthor
New Participant
March 8, 2023

Yes there are several proprietery methods I used. The methods essentially return a list of resources that match a particular resource type. If the given resource is of the same type, it returns that resource, otherwise, it navigates through the descendants of the given resource  to find all resources that match the given resource type.

New Participant
March 8, 2023

@anasustic did you found the solution for what you are looking? Even I need to fetch all the component from current page. Appreciate if you share the solution here.

Umesh_Thakur
New Participant
March 3, 2023

You can not directly adapt it from request. you should first get the resolver then from resolver you can adapt it to like in below code:

ResourceResolver resourceResolver = request.getResourceResolver();
  ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);

but components returned by this manage might not be visible to the underlying resource resolver. Consumers of this manager should call Component.isAccessible() prior of using it. 

like:

com.day.cq.wcm.api.components.Component delegate = componentManager.getComponent("Name_or_property_of_the_component");
          if (delegate != null && delegate.isAccessible()) {
            //your code
          }

 Hope this helps

Umesh Thakur

ChitraMadan
New Participant
March 2, 2023

Hi @anasustic ,

 

Try to get it through Resource Resolver

ResourceResolver resourceResolver = request.getResourceResolver();
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
Nitin_laad
Nitin_laadAccepted solution
New Participant
March 2, 2023

check this out - groovy script -

import com.day.cq.wcm.api.Page import com.day.cq.wcm.api.PageManager def pagePath = "/content/mysite/mypage" def pageManager = sling.getService(PageManager.class) def page = pageManager.getPage(pagePath) def componentType = "mysite/components/mycomponent" def components = page.contentResource?.children?.iterator() // Iterate over each component and check if it's of the specified type while (components?.hasNext()) { def component = components.next() if (component.resourceType == componentType) { println "Found component of type ${componentType} at path ${component.path}" } }