AEM JCR SQL2 query to find all pages contains specific component

2 min read 06-10-2024
AEM JCR SQL2 query to find all pages contains specific component


Finding Pages with Specific Components in AEM using JCR SQL2 Queries

AEM (Adobe Experience Manager) is a powerful content management system that uses the JCR (Java Content Repository) to store and manage content. Understanding how to query the JCR can be crucial for retrieving the information you need. In this article, we will explore how to use JCR SQL2 queries to find all pages that contain a specific component in AEM.

The Challenge: Finding Pages with Specific Components

Imagine you need to identify all pages on your AEM instance that contain a particular component, let's say a "Featured Products" component. You might need to perform this task for several reasons:

  • Updating or migrating a component: You might need to update all instances of the "Featured Products" component on your site.
  • Auditing your content: You might want to check if all pages with specific components are properly configured.
  • Troubleshooting content issues: You might be investigating a bug or performance issue related to a specific component.

The Solution: JCR SQL2 Queries

The JCR (Java Content Repository) provides a powerful query language called JCR SQL2, which allows you to perform complex queries on the content stored in the repository. Here's a basic JCR SQL2 query to find all pages containing the "Featured Products" component:

SELECT * FROM [jcr:root]
WHERE jcr:path LIKE '/content/your-site/%'
AND ISDESCENDANTNODE(jcr:path, '/content/your-site/your-page/your-component')

Explanation:

  • SELECT * FROM [jcr:root]: This selects all nodes under the root of the JCR repository.
  • WHERE jcr:path LIKE '/content/your-site/%': This filters the results to only include pages under the /content/your-site path. You should replace your-site with the actual name of your site.
  • AND ISDESCENDANTNODE(jcr:path, '/content/your-site/your-page/your-component'): This filters the results further to include only pages that have a descendant node at the specified path. Replace your-page with the path to a page that contains the "Featured Products" component and your-component with the actual path to the "Featured Products" component within the page.

Important Note: The specific path to your component may vary depending on your AEM project structure. It's essential to inspect your content in AEM to find the correct path.

Additional Tips and Considerations

  • Optimizing Performance: Using wildcard characters (like %) in your JCR SQL2 queries can impact performance. Try to use more specific filters whenever possible.
  • Querying for Specific Properties: You can further refine your query by checking for specific properties within the component. For example, to find pages with the "Featured Products" component and a specific product ID, you could use the following query:
SELECT * FROM [jcr:root]
WHERE jcr:path LIKE '/content/your-site/%'
AND ISDESCENDANTNODE(jcr:path, '/content/your-site/your-page/your-component')
AND @your-component/productID = 'your-product-id'
  • Using AEM Query Builder: AEM offers a built-in Query Builder UI that can help you create JCR SQL2 queries easily. You can access it through the Tools console in AEM.

Conclusion

By understanding how to use JCR SQL2 queries, you can easily locate pages containing specific components in your AEM instance. This knowledge will help you to efficiently manage your content, troubleshoot problems, and make necessary updates to your site.

Further Resources: