Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

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.

sumans4511174
Level 2
Level 2

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

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post

10 Replies
snbaem
Community Advisor
Community Advisor

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
Level 2
Thank you so much @snbaem for your response. But consider that i don't have path to any particular project. or you can assume that i don't know how many project folders are there in /conf. So given path is only "/conf" and i need to find content fragment models in all projects. So i am looking for that loop which first go into all the project folders one by one and then list down details for details for all content fragment models.
sumans4511174
Level 2
Level 2
I can't use query builder for this. Because see the result - The query read or traversed more than 100000 nodes. To avoid affecting other tasks, processing was stopped.
snbaem
Community Advisor
Community Advisor

In the query can you try adding type=cq:PageContent ? What query did you use? Not expecting to see such traversing numbers under /conf. Otherwise try to use the java looping by getting the /conf resource and add checks in the loop to ignore unnecessary resources/nodes

sumans4511174
Level 2
Level 2
path=/conf/ property=cq:templateType property.value=/libs/settings/dam/cfm/model-types/fragment
sumans4511174
Level 2
Level 2
path=/conf/ property=cq:templateType property.value=/libs/settings/dam/cfm/model-types/fragment This one is working... Thank you so much for your help.
SureshDhulipudi
Community Advisor
Community Advisor

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)

AEM-Content-Fragment-ResourceType.png

Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

View solution in original post