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

Avatar

Avatar

sumans4511174

Avatar

sumans4511174

sumans4511174

15-11-2020

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

Accepted Solutions (0)

Answers (3)

Answers (3)

Avatar

Avatar

Anudeep_Garnepudi

Avatar

Anudeep_Garnepudi

Anudeep_Garnepudi

18-11-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

Avatar

Avatar

sureshd71454102

Avatar

sureshd71454102

sureshd71454102

16-11-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)

AEM-Content-Fragment-ResourceType.png

Avatar

Avatar

snbaem

Avatar

snbaem

snbaem

15-11-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..