Dashboards & Visualizations

How does one access Splunk's global environment tokens from JavaScript extensions (via SplunkJS)?

rjthibod
Champion

Splunk 6.5 added global environment tokens that are accessible in SimpleXML (http://docs.splunk.com/Documentation/Splunk/6.5.0/Viz/tokens#Use_global_tokens_to_access_environment...).

My question is how does one obtain these token values from SplunkJS such as in a JavaScript extension to a SimpleXML dashboard?

Trying to obtain the values from defaultTokenModel or submittedTokenModel returns no result, and exploring those objects in the Chrome debugger does not indicate they would be defined in those objects.

I understand some of these values are available via rest API calls. I am specifically interested in getting them via SplunkJS.

1 Solution

ziegfried
Influencer

Here's an example:

require(['splunkjs/mvc', 'splunkjs/mvc/simplexml/ready!'], function(mvc) {

    var envTokenModel = mvc.Components.get('env');

    // Grab a specific env token
    console.log(envTokenModel.get('app'));

    // Log all env tokens
    console.log(envTokenModel.toJSON());

    // React to env token changes:
    envTokenModel.on('change', function() {
        console.log(arguments);
    });

});

View solution in original post

ziegfried
Influencer

Here's an example:

require(['splunkjs/mvc', 'splunkjs/mvc/simplexml/ready!'], function(mvc) {

    var envTokenModel = mvc.Components.get('env');

    // Grab a specific env token
    console.log(envTokenModel.get('app'));

    // Log all env tokens
    console.log(envTokenModel.toJSON());

    // React to env token changes:
    envTokenModel.on('change', function() {
        console.log(arguments);
    });

});

helge
Builder

It should REALLY be documented that you can access the env tokens (e.g. env:version) by specifying "env" as token model.

0 Karma

SK8
Explorer

Hello,
this solution run only for Simple-XML Dashboard. Exist a solution for a HTML-Dashboard to get the environment variables?

0 Karma

rjthibod
Champion

It should be very similar. I haven't tested this, but basing it off of some old HTML code I have.

...

var envTokenModel = mvc.Components.getInstance('env', {create: true});
var app_val = envTokenModel.get("app")
console.log(envTokenModel.get('app'));

...
0 Karma

rjthibod
Champion

Thanks @ziegfried. If you are still at Splunk, would you mind passing on a note to the docs team to incorporate this on the Dev portal at http://dev.splunk.com/view/webframework-developapps/SP-CAAAEW2?

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...