Expand my Community achievements bar.

SOLVED

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

2 Replies

Avatar

Community Advisor

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

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

Avatar

Correct answer by
Employee Advisor

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