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

[AEM Gems Webinar] Accelerating Experience Manager as a Cloud Service development with Rapid Development Environments
SOLVED

Fetch featured image from Page properties in AEM 6.5

Avatar

Level 3

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 @AvinashGupta01 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
Level 9

@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 3

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
Level 9

@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 3

Hi @AvinashGupta01 , 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

Level 3

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