Expand my Community achievements bar.

SOLVED

Fetch featured image from Page properties in AEM 6.5

Avatar

Level 5

This is the hierarchy of the page located under /content.

arindam6600_0-1668618859684.png

Now there's a property fileReference in the cq:featuredImage node and I want to fetch it in the sling model. How do I do it.

As mentioned by @Avinash_Gupta_ I've used the following piece of code to fetch the page title.

PageManager pm = resource.getResourceResolver().adaptTo(PageManager.class);
Page containingPage = pm.getContainingPage(resourceResolver.getResource(pagePath));
ValueMap pageProperties = containingPage.getProperties();
String pageTitle= pageProperties.get("jcr:title", String.class);

 

Can someone help me fetch the fileReference property located in cq:featuredImage into my sling model?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@arindam6600 : Please use the following lines of snippet to get the value of fileReference in the cq:featuredImage node along with the previous snippet of code:-

 

Resource featuredImageResource = containingPage.getContentResource("cq:featuredImage");
ValueMap properties = featuredImageResource.adaptTo(ValueMap.class);
String fileReference = properties.get("fileReference", String.class);

 

Try this snippet and let me know if you face any issue.

 

 

View solution in original post

7 Replies

Avatar

Level 5

Hi @SantoshSai ,

Thanks for the link, but I couldn't figure out much. Will it be possible for you to help me with the code snippet that I can put in my sling model?

Avatar

Correct answer by
Community Advisor

@arindam6600 : Please use the following lines of snippet to get the value of fileReference in the cq:featuredImage node along with the previous snippet of code:-

 

Resource featuredImageResource = containingPage.getContentResource("cq:featuredImage");
ValueMap properties = featuredImageResource.adaptTo(ValueMap.class);
String fileReference = properties.get("fileReference", String.class);

 

Try this snippet and let me know if you face any issue.

 

 

Avatar

Level 5

Hi @Avinash_Gupta_ , I tried with the above script but it just prints cq:featuredimage and not the path of the image although the path is shown when checked in crx. Please advice.

Avatar

Community Advisor

@arindam6600 Can you share me the screenshot of the CRXDE which node and property you want to retrieve?

Avatar

Level 5

Hi @Avinash_Gupta_ thanks for the help. The above piece of code worked. I did some typo and hence it was throwing errors.

Avatar

Level 5

How would you go about getting the alt text from the DAM linked asset?