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
Validate 1
Level 1
sumans4511174
Level 1

Like

1 like

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
sumans4511174
Level 1

Like

1 like

Total Posts

12 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
sumans4511174
Level 1

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 (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

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

Answers (2)

Answers (2)

Avatar

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile

Avatar
Ignite 1
MVP
SureshDhulipudi
MVP

Likes

163 likes

Total Posts

174 posts

Correct reply

49 solutions
Top badges earned
Ignite 1
Give Back 5
Give Back 3
Give Back 10
Give Back
View profile
SureshDhulipudi
MVP

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
Boost 100
Level 6
snbaem
Level 6

Likes

115 likes

Total Posts

246 posts

Correct reply

37 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile

Avatar
Boost 100
Level 6
snbaem
Level 6

Likes

115 likes

Total Posts

246 posts

Correct reply

37 solutions
Top badges earned
Boost 100
Springboard
Establish
Validate 25
Validate 10
View profile
snbaem
Level 6

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