How to inject page property into Sling Model using annotations | Community
Skip to main content
bruno-bradach
September 15, 2023
Solved

How to inject page property into Sling Model using annotations

  • September 15, 2023
  • 3 replies
  • 2458 views

Hi everyone!

 

I would like to inject a page property into a Sling Model using annotations but I'm struggling. Like @ValueMapValue but for a page property.

 

Any ideas on how to do it?

Thanks!

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

Hi @bruno-bradach 

 

Page property is associated with the page and not to a component so I believe you cannot directly use inject or valuemapvalue to read the property.

 

However you can use the below annotations

@ScriptVariable
private ValueMap pageProperties;

@SlingObject
private Page currentPage;

 

which will read the properties and use the get method and retrieve the required property from the page.

 

Hope it helps 

3 replies

sherinregi-1
Community Advisor
sherinregi-1Community AdvisorAccepted solution
Community Advisor
September 15, 2023

Hi @bruno-bradach 

 

Page property is associated with the page and not to a component so I believe you cannot directly use inject or valuemapvalue to read the property.

 

However you can use the below annotations

@ScriptVariable
private ValueMap pageProperties;

@SlingObject
private Page currentPage;

 

which will read the properties and use the get method and retrieve the required property from the page.

 

Hope it helps 

aanchal-sikka
Community Advisor
Community Advisor
September 15, 2023

Hello @bruno-bradach 

 

Sharing a cheat sheet for different annotations used in Sling Models (Including ValueMap).

https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide

https://techrevel.blog/2017/03/18/sling-model-annotations/

 

It will help with current and later use-cases as well

Aanchal Sikka
Adobe Employee
October 13, 2023