Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to read the count of components?

Avatar

Level 4

Hi All,

i want to read the count of the components which is repeated component.

Example:  RootContentNode/jcr:content/pageContent/header/

                                                                             /header_1

                                                                            /header_2  

The above example the header component repeated as three times, so i want to get the count as 3.

thanks in advance

michael

Could you please help me out. 

1 Accepted Solution

Avatar

Correct answer by
Level 10

You will have to write a custom logic (Purely Java) using JCR API to get the count of the repeated component

Logic 1: If you want to find the components which is repeated, then get the list of component names from apps and write a search query for each of the component name. Based on the result set see if you see multiple results for a same node.

Logic 2: Under /content loop through each of the node under jcr:content and see if the node *like* current node name exists or not. If yes, then that component is repeated in a given page node.

View solution in original post

3 Replies

Avatar

Level 4

Hi Michael,

Try this.

        long count = 0;
        QueryManager qm;
        try {
            String compPath = "project/components/header";//example : project/components/header
            String searchQuery = "SELECT * FROM [nt:unstructured] AS s WHERE ISDESCENDANTNODE(["+currentPage.getPath()+"/jcr:content"+"]) and [sling:resourceType]='"+compPath+"'";
            qm = resourceResolver.adaptTo(Session.class).getWorkspace().getQueryManager();
            Query query = qm.createQuery(searchQuery, Query.JCR_SQL2);
            QueryResult result = query.execute();
            count = result.getRows().getSize();//component count
        } catch (RepositoryException e) {            
        }
 

Avatar

Correct answer by
Level 10

You will have to write a custom logic (Purely Java) using JCR API to get the count of the repeated component

Logic 1: If you want to find the components which is repeated, then get the list of component names from apps and write a search query for each of the component name. Based on the result set see if you see multiple results for a same node.

Logic 2: Under /content loop through each of the node under jcr:content and see if the node *like* current node name exists or not. If yes, then that component is repeated in a given page node.

Avatar

Administrator

Adding to Lokesh's comment. Please find below the community articles for using JCR API

Link:- https://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html (Querying Adobe Experience Manager Data using the JCR API)

Link:- https://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html (Programmatically Accessing Adobe CQ Content using the JCR API)

 

Thanks and Regards

Kautuk Sahnia



Kautuk Sahni