Component Usage Script for AEM Fiddle

Diana Henrickson
Activate AEM
Published in
1 min readMay 30, 2023

This script will give you a list of where a component is currently used on our site.

Grab the code below, copy and paste it into a AEM Fiddle (java) file and set the following information based on your needs:

  • contentPath — Location to search for the component usage
  • componentResourceType — The resource type of the component to search for
package apps.acs_002dtools.components.aemfiddle.fiddle;

import com.day.cq.search.*;
import com.day.cq.wcm.api.*;
import com.day.cq.dam.api.*;
import org.apache.sling.api.*;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.servlets.*;
import java.io.IOException;
import javax.jcr.*;
import java.util.*;
import org.apache.sling.api.resource.ResourceResolver;

public class fiddle extends SlingAllMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

String componentResourceType = "wknd/components/breadcrumb";
String contentPath = "/content/wknd";
ResourceResolver resolver = request.getResourceResolver();
String query = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE([" + contentPath
+ "]) AND [sling:resourceType]='" + componentResourceType + "'";
Iterator<Resource> resources = resolver.findResources(query, "JCR-SQL2");
response.getWriter().println("Path<br>");
while (resources.hasNext()) {
Resource componentInstance = resources.next();
String path = componentInstance.getPath();

response.getWriter().println(path+"<br>");
}

}
}

--

--