Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

How to identify preview environment via code in aemaacs

Avatar

Level 1

Hi team,

 

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

6 Replies

Avatar

Level 3

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>

Avatar

Level 1

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?

Avatar

Level 3

@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

Avatar

Community Advisor

@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

 

Avatar

Community Advisor

@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