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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

How can I get a property for a content fragment?

Avatar

Level 2

Hello.

 

I have some content fragments and I need to get the cq:parentPath value, which I see here:

 

jeremyc82321732_0-1674678982641.png

 

I have tried something like this, but the ContentElement is null

 

ContentElement test = resultResource.adaptTo(ContentFragment.class).getElement("jcr:content/cq:parentPath");

 

How can I find this value?

 

Thanks!

 

Jeremy

2 Replies

Avatar

Level 8

You can resource API itself to read the property of the content fragment.

resultResource.getChild("jcr:content").getValueMap().get("cq:parentPath", String.class)

Avatar

Employee

Hi @jeremyc82321732 ,

 

You are getting this as null because the "getElement()" would return the properties of the content fragment from the data node

"<contentfragmentpath>/jcr:content/data/master".

 

API References:-

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/...

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/adobe/cq/dam/cfm/... 

To fetch data from the jcr:content node, you can adapt it to the resource and then read it from the value map properties.

 

 

Resource res = resourceResolver.resolve("<path-to-cf>/jcr:content");
ValueMap valueMap = res.getValueMap();
String parentPath = valueMap.get("cq:parentPath", String.class);

 

 

Hope this helps!

 

Regards,

Nitesh