How can I get a property for a content fragment? | Community
Skip to main content
jeremyc82321732
Level 2
January 25, 2023
Solved

How can I get a property for a content fragment?

  • January 25, 2023
  • 2 replies
  • 1792 views

Hello.

 

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

 

 

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

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 nitesh_kumar-1

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/ContentFragment.html

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

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

2 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
January 25, 2023

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

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

nitesh_kumar-1
Adobe Employee
nitesh_kumar-1Adobe EmployeeAccepted solution
Adobe Employee
January 25, 2023

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/ContentFragment.html

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

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