How to identify preview environment via code in aemaacs | Community
Skip to main content
Level 2
April 11, 2024

How to identify preview environment via code in aemaacs

  • April 11, 2024
  • 3 replies
  • 1863 views

Hi team,

 

May I know how to identify preview environment programmatically in aem as a. Cloud service.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

RikVanB
Level 2
April 11, 2024

Hi Kroyal

 

Not sure if you want to identify it in the Java Model or in the view, below you can find both options.

 

Java Sling Model

You can inject SlingHttpServletRequest in your model and get the WCMMode out of it like this

 

WCMMode mode = WCMMode.fromRequest(request);
if(mode == WCMMode.PREVIEW) {
// Insert Logic
}

 

View

In the view the wcmmode is globally available, so it's enough to add following check

 

<sly data-sly-test.isPreview="${wcmmode.preview}">
<!-- Print HTML for Preview Mode -->
</sly>
kroyal12Author
Level 2
April 11, 2024

Hi @rikvanb 

Thanks for replying, I have asked for preview environment not for preview mode,

 

Could you please confirm if it works for preview environment in cloud?

RikVanB
Level 2
April 11, 2024

@kroyal12Sorry, misread the question...

 

I am affraid that it's not that easy...

I don't think there is a way to do that out-of-the-box, what you could do it create an OSGi Configuration with a Service which is using a environment variable that is configured through Cloud Manager on the preview environments.

 

For example, you can define a variable IS_PREVIEW_ENV, and use that variable in your OSGi Config.

 

Documentation for configuring a environment variable through Cloud Manager you can find here.

Example on how to use those environment variables in your OSGi Configuration you can find here.

 

Sure, not the answer you hoped for, but it's the best I could do... Maybe someone else has a beter solution for you!

 

Greetings

Rik

Saravanan_Dharmaraj
Community Advisor
Community Advisor
April 11, 2024

@kroyal12  Are you talking about finding environment runmode in cloud? Or just WCMMODE = Preview ?

 

https://www.theaemmaven.com/post/the-slingsettingsservice-run-modes-in-aemaacs

 

kroyal12Author
Level 2
April 11, 2024

Talking about environment 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 18, 2024

@kroyal12 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Esteban Bustamante