How to read the count of components? | Community
Skip to main content
mikezooz
Level 4
February 16, 2016
Solved

How to read the count of components?

  • February 16, 2016
  • 3 replies
  • 4372 views

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. 

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 Lokesh_Shivalingaiah

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.

3 replies

vikramca06
Level 4
February 16, 2016

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) {            
        }
 

Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
February 16, 2016

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.

kautuk_sahni
Community Manager
Community Manager
February 17, 2016

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