I want to list details of all content fragment models available in the conf folder. should i do with sling api or jcr api. | Community
Skip to main content
sumans4511174
Level 2
November 15, 2020
Solved

I want to list details of all content fragment models available in the conf folder. should i do with sling api or jcr api.

  • November 15, 2020
  • 3 replies
  • 4702 views

I want to list details of all content fragment models present in conf folder. if anybody can help me the loop which i need to reach the content model nodes. That would be great

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 Anudeep_Garnepudi

Hi @sumans4511174 

You and get by executing JCR SQL2 query.

Firstly get JCR Session from either ResourceResolver or JcrUtils(depends on your implementation).

Get QueryManager then create and execute Query. Check below code example.

QueryManager qm = session.getWorkspace().getQueryManager();
String queryString = "SELECT * FROM [cq:PageContent] AS comp WHERE ISDESCENDANTNODE(comp, \"/conf\") AND [cq:templateType] = \"/libs/settings/dam/cfm/model-types/fragment\"";
Query query = qm.createQuery(queryString, Query.JCR_SQL2);
QueryResult queryResult = query.execute();
RowIterator rowItr = queryResult.getRows();
while(rowItr.hasNext()){
   ...
}

Hope this is what you are looking for. Cheers!

AG

3 replies

Shubham_borole
Community Advisor
Community Advisor
November 15, 2020

You could do a query like below. Adjust params as required

path=/conf/project/settings/dam/cfm/models property=sling:resourceType property.value=dam/cfm/models/console/components/data/entity/default

 or 

path=/conf/project/settings/dam/cfm/models property=cq:templateType property.value=/libs/settings/dam/cfm/model-types/fragment

 For looping 

can do something like

Resource confProjectRes = readServiceResolver.resolve("/conf/myproject/settings/dam/cfm/models"); List<Resource> list = new ArrayList<>(); //Once you get the above resource, pass it as rootResource in below method. And list as desiredResources public void getChildren(List<Resource> desiredResources, Resource rootResource, final String resourceType) { Iterator<Resource> directChildren = rootResource.listChildren(); while (directChildren.hasNext()) { Resource childRes = directChildren.next(); if (childRes.isResourceType(resourceType)) { desiredResources.add(childRes); } else { getChildren(desiredResources, childRes, resourceType); } } }

 

Once you get all the resources, you can  get details about the models. Not sure if this is the intention or what the final requirement is but above ways can help getting the CF model nodes as resources.
Unable to find an API to provide available models.. will update in such case..

sumans4511174
Level 2
November 16, 2020
Thank you so much @shubham_borole for your response.
SureshDhulipudi
Community Advisor
Community Advisor
November 16, 2020

if you want to query under /conf, then you can add a condition with Resource Type

 

isResourceType("dam/cfm/models/console/components/data/entity/default")

 

Check the Content Fragment model properties (ResourceType and ResourceSuper Type)

Anudeep_Garnepudi
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
November 18, 2020

Hi @sumans4511174 

You and get by executing JCR SQL2 query.

Firstly get JCR Session from either ResourceResolver or JcrUtils(depends on your implementation).

Get QueryManager then create and execute Query. Check below code example.

QueryManager qm = session.getWorkspace().getQueryManager();
String queryString = "SELECT * FROM [cq:PageContent] AS comp WHERE ISDESCENDANTNODE(comp, \"/conf\") AND [cq:templateType] = \"/libs/settings/dam/cfm/model-types/fragment\"";
Query query = qm.createQuery(queryString, Query.JCR_SQL2);
QueryResult queryResult = query.execute();
RowIterator rowItr = queryResult.getRows();
while(rowItr.hasNext()){
   ...
}

Hope this is what you are looking for. Cheers!

AG