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
BedrockMission!

Learn More

View all

Sign in to view all badges

Get runmodes value in client-side JS

Avatar

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile
hanscy
Level 2

15-03-2019

Hi guys,

is there a way in AEM to get a value from AEM runmodes in client-side JS?

I have a key value that is changing depending on the environment, it needs to be on client-side JS since it is calling a widget integration.

Thanks before!

Replies

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

449 likes

Total Posts

1,024 posts

Correct Reply

116 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

449 likes

Total Posts

1,024 posts

Correct Reply

116 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

15-03-2019

The runmode values are something you can get either via server side Java API or using Server Side JS.

Now I don't know if this is the right way to do it , but one thing what is coming to mind right now is to add the runmode as a hidden value in an html element in your page and then using the client side JS , fetch value from the element . I would really love to see other expert opinion on this .

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,279 likes

Total Posts

3,153 posts

Correct Reply

889 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,279 likes

Total Posts

3,153 posts

Correct Reply

889 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

15-03-2019

Hi,

To manage/configure run mode specific value, you should use run mode osgi configuration and read key value using model.and store either in HTML hidden element or JSON object or use it if applicable. It will remove the extra run mode check overhead.

But if you want to check run mode, you have to go with server side JS Api or Java.

e.g.

template.html

<div data-sly-use.head="logic.js">

<p><b>Modes :</b> ${head.runmodes}</p>

<p><b>Is This author :</b> ${head.author}</p>

<p><b>Is This Publish :</b> ${head.publish}</p>

</div>

logic.js

var SlingSettingsService = Packages.org.apache.sling.settings.SlingSettingsService;

use(function () {

    // Get runmodes and transform them into an object that is easier to read for Sightly

    var runmodesObj = {};

    var runmodesSet = sling.getService(SlingSettingsService).getRunModes();

    var iterator = runmodesSet.iterator();

    var isauthor=sling.getService(SlingSettingsService).getRunModes().contains("author");

    var ispublish=sling.getService(SlingSettingsService).getRunModes().contains("publish");

    while (iterator.hasNext()) {

        runmodesObj[iterator.next()] = true;

    }

   

    return {

        runmodes: runmodesObj,

        author: isauthor,

        publish: ispublish

    }

});

Avatar

Avatar
Give back 300
MVP
Gaurav-Behl
MVP

Likes

243 likes

Total Posts

1,145 posts

Correct Reply

281 solutions
Top badges earned
Give back 300
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Give back 300
MVP
Gaurav-Behl
MVP

Likes

243 likes

Total Posts

1,145 posts

Correct Reply

281 solutions
Top badges earned
Give back 300
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile
Gaurav-Behl
MVP

15-03-2019

Use 'wcmmode' or 'cq-authoring-mode' cookie per your use case. Add appropriate null/undefined checks in place.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

15-03-2019

The runmode is an implementation detail, and you should tie your JS code directly to that. Instead set config value with OSGI configuration, and expose the value of this configuration somehow (render it into a page or via JSON).

Avatar

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile
hanscy
Level 2

17-03-2019

Thanks Jörg!

when you said render it into a page? do you mean put it on the HTML as a hidden as what @Veena_07 ​stated?

Avatar

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile

Avatar
Boost 3
Level 2
hanscy
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

0 solutions
Top badges earned
Boost 3
Boost 1
View profile
hanscy
Level 2

17-03-2019

Hi Veena!

Exactly I was thinking the same way as you do putting it on HTML as a hidden value. However I don't really feel that it is very correct approach

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,279 likes

Total Posts

3,153 posts

Correct Reply

889 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,279 likes

Total Posts

3,153 posts

Correct Reply

889 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

18-03-2019

Hi,

I don't think so there is any harm if you want to expose some value in hidden field or as JSON/javascript variable based on run mode at client side. If you expose all the run mode then that could be the vulnerability.

I would suggest do all the calculation based on run mode at server side and base on outcome set different/boolean variable.

Or store key in osgi config and access directly.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

18-03-2019

You should not expose the concept of runmodes to your frontend. Instead you should define a marker which is queried by your frontend; and this marker might be rendered directly into the page or queried via JSON. And the presence of the marker in the markup (or JSON) is controlled by an OSGI configuration which can be set via runmode-based OSGI configuration.

Otherwise you need to have knowledge in your frontend about backend specifics.