Retrieve dashboard list

🟢 Beginner

script dashboard

This tutorial demonstrates how to retrieve the list of all dashboards using a script and store it in a variable. The script will be executed every 10 seconds.

  • Write a detached script that calls the dashboard API and stores the result in a variable.

  • Define a text variable using value.ospp and owner.variables to hold the dashboard list.

  • Create two test dashboards (main and alternative) to verify the script output.

  • Display the retrieved dashboard IDs on a dashboard using the valueSubscriptions widget.

git checkout origin/osp-scripts-configuration .
git checkout origin/osp-variables-configuration .
git checkout origin/osp-web-configuration .
git checkout origin/example-scripts-get-dashboards-list .

Steps

1. Create a script to retrieve the dashboard list and store it in a variable

root/scripts/script.js

const result = config.getDashboards()

if(result) {

    const content = result.content;

    if (content && content.dashboards) {
        log.info("Dashboards found: {}", content.dashboards.length);
        const dashboards = content.dashboards;

        const idList = dashboards.map(d => d.id).join(',');
        log.info("Dashboard IDs: {}", idList);
        values.update("root.variable", idList);

    } else 
    {
        log.warn("No dashboards found.");

    }
} else {
    log.warn("Failed to retrieve dashboards.");
}

root/scripts/detached.scripts

{
    "accessedValues": [],
    "scheduledExecutions" : ["0/10 * * ? * * *"],
    "sourceFile": "root/scripts/script.js",
    "moduleId": "modules.scripts.scripts-1"
}

root/scripts/output.scripts

{
    "scriptId": "root.scripts"
}

2. Define a text variable to store the dashboard list

root/scripts/variable/value.ospp

{
    "name": "Dashboard list",
    "description": "List of available dashboards",
    "type": "TEXT"
}

root/scripts/variable/owner.variables

{
    "moduleId": "modules.variables.variables-1"
}

3. Create two dashboards

Create two dashboards named main and alternative. These will be used to test the script.

root/scripts/dashboards/main/dashboard.view

{
    "configuration": [
        {
            "valueSubscriptions": {
                "values": [
                    {
                        "id": "root.variable",
                        "right": "READ",
                        "type": "TEXT"
                    }
                ]
            },
            "id": "PoNOGOxx",
            "type": "ValueSubscription",
            "title": ""
        }
    ],
    "layout": {
        "lg": [
            {
                "w": 12,
                "h": 4,
                "x": 0,
                "y": 0,
                "i": "PoNOGOxx"
            }
        ]
    },
    "breakpoints": {
        "lg": 1200,
        "md": 996,
        "sm": 768,
        "xs": 480,
        "xxs": 0
    },
    "cols": {
        "lg": 12,
        "md": 10,
        "sm": 6,
        "xs": 4,
        "xxs": 2
    },
    "rowHeight": 150
}

root/scripts/dashboards/main/dashboard.web

{
    "moduleId": "modules.web.web-1",
    "title": "Main Dashboard",
    "description": "Main OSP Dashboard",
    "tags": []
}

root/scripts/dashboards/alternative/dashboard.view

{
    "configuration": [
    ],
    "layout": {
        "lg": []
    }
}

root/scripts/dashboards/alternative/dashboard.web

{
    "moduleId": "modules.web.web-1",
    "title": "Alternative Dashboard",
    "description": "Alternative OSP Dashboard",
    "tags": []
}

4. View results

After the script runs, the variable will contain the list of dashboard IDs. On the main dashboard, the value can be displayed using the valueSubscriptions widget.

Additionally, the script module logs will show the list of retrieved dashboard IDs.