Skip to content

Back to the cookbook

Apply a theme to a dashboard

Applies a named dashboard theme to a dashboard.

Adam Robertson Dashboard themesDashboards

How it works

  1. Log in and capture the access token.
  2. List all themes, find the one whose name matches ThemeName, and capture its ID.
  3. Call GetDashboard.ps1 to get the dashboard's summary, which includes lastUpdateId.
  4. Call GetDashboardUpdate.ps1 with that update ID to get the full JSON definition.
  5. Check the current themeId in the definition. If it already matches the target, stop.
  6. Convert the JSON to an object, set themeId to the theme's ID, and convert it back.
  7. Write the modified JSON to a temporary file, and call UpdateDashboard.ps1 to post it.
  8. Delete the temporary file.

Warning

This script modifies a dashboard in place. Test against a non-production system first.

Scripts it uses

Before you start

You need:

  • The base URL of the Orbit API (see Find your API URLs)
  • An Orbit username and password for the DataView

This script runs other scripts in the repository, so download all of them and run SetDashboardTheme.ps1 from the Powershell folder. See Get the scripts.

Parameters

Parameter Description
-BaseUrl The base URL of the Orbit API, for example https://example.com/OrbitAPI
-DataViewName The name of the DataView to act on, for example holidays
-Username The username to authenticate with.
-DashboardId The ID of the dashboard to update.
-ThemeName The name of the theme to apply, exactly as it appears in the Orbit theme list.

Example

PowerShell
.\Workflows\SetDashboardTheme.ps1 `
    -BaseUrl      "https://example.com/OrbitAPI" `
    -DataViewName "holidays" `
    -Username     "myuser" `
    -DashboardId  42 `
    -ThemeName    "Corporate Blue"

Example output

PowerShell session
> .\Workflows\SetDashboardTheme.ps1 `
      -BaseUrl      "https://example.com/OrbitAPI" `
      -DataViewName "holidays" `
      -Username     "myuser" `
      -DashboardId  42 `
      -ThemeName    "Corporate Blue"

Authenticating as 'myuser' against '...'...
Login successful. Access token obtained.
Getting dashboard themes...
Got 3 dashboard themes
Found theme 'Corporate Blue' with id 7.
Getting dashboard 42 for user myuser...
Got dashboard 42: Impact Report Last Month
Getting dashboard update 91 for dashboard 42...
Got dashboard update 91 for dashboard 42: Impact Report Last Month
Dashboard 'Impact Report Last Month' current theme: default (0). Changing to 'Corporate Blue' (id 7)...
Dashboard 'Impact Report Last Month' updated successfully with 4 tabs!
Dashboard 'Impact Report Last Month' theme updated to 'Corporate Blue'.

Notes

  • Changing one property: because the script targets one property (themeId) rather than doing a broad text substitution, it's safer to parse the JSON with ConvertFrom-Json, set the property directly, and convert it back with ConvertTo-Json -Depth 100. Only the intended field changes.
  • A themeId of 0 means the DataView default: a value of 0 isn't an invalid ID. It's how the API signals that the dashboard uses the DataView's default theme. To go back to the default, set ThemeName to the theme currently marked [DEFAULT], or change the target theme ID in the script to 0.
  • Theme names match case-insensitively: the comparison uses PowerShell's -eq operator, which is case-insensitive. If your theme names differ only in case, use -ceq instead.

Script

The script from the repository at commit b732915. Its built-in help is left out here, because this page shows it above. The download and the copy on GitHub include it.

Warning

Apteco provides these scripts as examples only. They're not officially supported. Review each script and test it on a non-production system before you run it against live data.

PowerShell
param (
    [Parameter(Mandatory = $true)]
    [string]$BaseUrl,

    [Parameter(Mandatory = $true)]
    [string]$DataViewName,

    [Parameter(Mandatory = $true)]
    [string]$Username,

    [Parameter(Mandatory = $true)]
    [int]$DashboardId,

    [Parameter(Mandatory = $true)]
    [string]$ThemeName
)

# The building-block scripts live one folder up, in the Powershell folder
$scripts = Split-Path -Parent $PSScriptRoot

# ---------------------------------------------------------------------------
# Step 1: Log in and get an access token
# ---------------------------------------------------------------------------
$token = & "$scripts\Login.ps1" -BaseUrl $BaseUrl -DataViewName $DataViewName -Username $Username

if ([string]::IsNullOrWhiteSpace($token)) {
    Write-Error "Login failed - no token returned."
    exit 1
}

# ---------------------------------------------------------------------------
# Step 2: List all themes and find the ID for the requested theme name
#
# ListDashboardThemes.ps1 writes one line per theme in the format:
#   <id>: <name>
# or, for the default theme:
#   <id>: <name> [DEFAULT]
# We strip the optional [DEFAULT] suffix before comparing names.
# ---------------------------------------------------------------------------
$themeLines = & "$scripts\DashboardThemes\ListDashboardThemes.ps1" -BaseUrl $BaseUrl -DataViewName $DataViewName -AccessToken $token

$targetThemeId = $null
foreach ($line in $themeLines) {
    if ($line -match '^(\d+):\s+(.+?)(\s+\[DEFAULT\])?$') {
        if ($Matches[2] -eq $ThemeName) {
            $targetThemeId = [int]$Matches[1]
            break
        }
    }
}

if ($null -eq $targetThemeId) {
    Write-Error "Theme '$ThemeName' not found. Run ListDashboardThemes.ps1 to see available themes."
    exit 1
}

Write-Host "Found theme '$ThemeName' with id $targetThemeId." -ForegroundColor Cyan

# ---------------------------------------------------------------------------
# Step 3: Get the dashboard summary to find the current lastUpdateId
#
# GetDashboard.ps1 outputs the dashboard object as JSON to the pipeline.
# ---------------------------------------------------------------------------
$dashboardJson = & "$scripts\Dashboards\GetDashboard.ps1" `
                    -BaseUrl      $BaseUrl `
                    -DataViewName $DataViewName `
                    -AccessToken  $token `
                    -DashboardId  $DashboardId | Out-String

$dashboard    = ConvertFrom-Json $dashboardJson
$lastUpdateId = $dashboard.lastUpdateId
$title        = $dashboard.title

# ---------------------------------------------------------------------------
# Step 4: Fetch the full update definition
#
# GetDashboardUpdate.ps1 outputs the update object as JSON to the pipeline.
# ---------------------------------------------------------------------------
$updateJson = & "$scripts\Dashboards\GetDashboardUpdate.ps1" `
                -BaseUrl           $BaseUrl `
                -DataViewName      $DataViewName `
                -AccessToken       $token `
                -DashboardId       $DashboardId `
                -DashboardUpdateId $lastUpdateId | Out-String

$update = ConvertFrom-Json $updateJson

# ---------------------------------------------------------------------------
# Step 5: Check the current theme and skip if already correct
#
# A themeId of 0 means the dashboard is using the DataView default theme.
# ---------------------------------------------------------------------------
$currentThemeId = $update.themeId
$currentLabel   = if ($currentThemeId -eq 0) { "default (0)" } else { $currentThemeId }

if ($currentThemeId -eq $targetThemeId) {
    Write-Host "Dashboard '$title' is already using theme '$ThemeName' (id $targetThemeId). No change needed." -ForegroundColor Yellow
    exit 0
}

Write-Host "Dashboard '$title' current theme: $currentLabel. Changing to '$ThemeName' (id $targetThemeId)..." -ForegroundColor Cyan

# ---------------------------------------------------------------------------
# Step 6: Update the themeId property and re-serialise
#
# Because we're changing a specific property rather than doing a text
# substitution, we deserialise the JSON, set the property, then re-serialise.
# This avoids accidentally replacing the same value elsewhere in the document.
# ---------------------------------------------------------------------------
$update.themeId = $targetThemeId
$modifiedJson   = ConvertTo-Json -InputObject $update -Depth 100

# ---------------------------------------------------------------------------
# Step 7: Write to a temp file and post the update
# ---------------------------------------------------------------------------
$tempFile = [System.IO.Path]::GetTempFileName()
try {
    Set-Content -Path $tempFile -Value $modifiedJson -Encoding UTF8

    & "$scripts\Dashboards\UpdateDashboard.ps1" `
        -BaseUrl           $BaseUrl `
        -DataViewName      $DataViewName `
        -AccessToken       $token `
        -DashboardId       $DashboardId `
        -DashboardJsonPath $tempFile
}
finally {
    Remove-Item $tempFile -ErrorAction SilentlyContinue
}

Write-Host "Dashboard '$title' theme updated to '$ThemeName'." -ForegroundColor Green

Back to the cookbook