Example: Search controls using tokens and JavaScript for apps outside Splunk Web

This example uses JavaScript to sync search controls to a search manager using token variables.

To use this code, save it as an HTML file in your own web site's directory. Be sure to update the splunkjs.config section for your own web app. The authenticate function redirects to a login_form.html example login page, which you can find here. For more, see Use SplunkJS Stack in your own web apps.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Search Controls: Tokens</title>

    <link rel="stylesheet" type="text/css" href="static/splunkjs/css/bootstrap.css" />
    <style>
        .main-area {
            padding: 30px;
        }
    </style>
</head>

<body>
    <div>
        <div class="main-area">
            <p>This example shows how to set up SearchBar and SearchControls views using Django tags, and sync them with a SearchManager using tokens.</p>
            <p><b>Note:</b> The Timeline view can't be synced using tokens. Use events instead.</p>

            <div id="mysearchbar1"></div>
            <div id="mysearchcontrols1"></div>
            <div id="mytable1"></div>
        </div>
    </div>

    <script src="static/splunkjs/config.js"></script>

    <script>
        // Configure SplunkJS Stack
        splunkjs.config({
            proxyPath: "/proxy",
            scheme: "https",
            host: "localhost",
            port: 8089,
            authenticate: function(done) { 
                require([
                    "jquery",
                    "jquery.cookie"
                ], function($) {
                    // Retrieve the session key and username from cookies
                    var splunkSessionKey = $.cookie("splunk_sessionkey");
                    var splunkCurrentUser = $.cookie("splunk_username");

                    // Log in using the session key and username
                    if (splunkSessionKey) {
                        done(null, {sessionKey: splunkSessionKey, username: splunkCurrentUser}); 
                    }
                    // If there is no session key, redirect to the login form
                    else {
                        window.location.replace("login_form.html");
                    }
                });
            }
        });
        
        // Configure the web site's base URL
        require.config({
            baseUrl: "static/"
        });

        // Set up the Web Framework components
        var deps = [
            "splunkjs/ready!",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/searchbarview",
            "splunkjs/mvc/searchcontrolsview",
            "splunkjs/mvc/tableview"
        ];
        require(deps, function(mvc) {
            // Load individual components
            var SearchManager = require("splunkjs/mvc/searchmanager");
            var SearchbarView = require("splunkjs/mvc/searchbarview");
            var SearchControlsView = require("splunkjs/mvc/searchcontrolsview");
            var TableView = require("splunkjs/mvc/tableview");

            // Create the search manager and views
            var mysearch = new SearchManager({
                id: "search1",
                search: mvc.tokenSafe("$searchquery$"),
                earliest_time: mvc.tokenSafe("$earlyval$"),
                latest_time: mvc.tokenSafe("$lateval$"),
                app: "search",
                preview: true,
                required_field_list: "*",
                status_buckets: 300
            });

            var mysearchbar = new SearchbarView({
                id: "searchbar1",
                managerid: "search1",
                value: mvc.tokenSafe("$searchquery$"),
                timerange_earliest_time: mvc.tokenSafe("$earlyval$"),
                timerange_latest_time: mvc.tokenSafe("$lateval$"),
                default: "index=_internal | head 100",
                el: $("#mysearchbar1")
            }).render();

            var mysearchcontrols = new SearchControlsView({
                id: "searchcontrols1",
                managerid: "search1",
                el: $("#mysearchcontrols1")
            }).render();

            var mytable = new TableView({
                id: "table1",
                managerid: "search1",
                el: $("#mytable1")
            }).render();
        });
    </script>
</body>
</html>