Expand my Community achievements bar.

How to identify preview environment via code in aemaacs

Avatar

Level 2

Hi team,

 

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

8 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 2

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

Level 1

Having the same issue right now. 

 

The OSGI configuration of preview is the same as the one of publish. At least this document states:

 

config.preview OSGi configuration folder cannot be declared in the same way a config.publish can be declared folder. Instead, the preview tier inherits its OSGi configuration from the publish tier's values.

 

So I guess the recommendation will not work.

 

@kroyal12 Did you find a solution?

Avatar

Community Advisor

@mo76 I think you did not understand what @kroyal12 explained, you are not defining a runmode for "preview", you won't have something like "config.preview", instead, you are defining an Environment Variable which will change its value only in the preview environment, please read carefully here: https://experienceleague.adobe.com/en/docs/experience-manager-cloud-service/content/implementing/usi... 

 

EstebanBustamante_0-1732195477197.png

Hope this helps



Esteban Bustamante

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