Skip to content

Back to the cookbook

Update a dashboard from a JSON definition

Updates a dashboard from a JSON definition file.

Adam Robertson Dashboards

How it works

Uses a valid access token to POST a JSON file to /{dataViewName}/Dashboards/{dashboardId}/Updates.

Before you start

You need:

This script works on its own: download it, plus Login.ps1 to get an access token.

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
-AccessToken A valid access token (gained from the Login.ps1 script) to authenticate with.
-DashboardId The ID of the dashboard to update.
-DashboardJsonPath The path to the JSON file to post to the Dashboards endpoint.

Example

PowerShell
.\UpdateDashboard.ps1 `
    -BaseUrl "https://example.com/OrbitAPI" `
    -DataViewName "holidays" `
    -AccessToken "your_access_token_here" `
    -DashboardId 1234 `
    -DashboardJsonPath ".\dashboard.json"

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
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string]$BaseUrl,

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

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

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

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

# ---------------------------------------------------------------------------
# Helper: exit with a clear error message
# ---------------------------------------------------------------------------
function Stop-WithError {
    param([string]$Message)
    Write-Error $Message
    exit 1
}

# ---------------------------------------------------------------------------
# Validate the dashboard JSON file exists
# ---------------------------------------------------------------------------
if (-not (Test-Path -Path $DashboardJsonPath -PathType Leaf)) {
    Stop-WithError "Dashboard JSON file not found: $DashboardJsonPath"
}

# ---------------------------------------------------------------------------
# Post the dashboard update JSON
# ---------------------------------------------------------------------------
$dashboardUrl  = "$BaseUrl/$DataViewName/Dashboards/$DashboardId/Updates"
$dashboardJson = Get-Content -Path $DashboardJsonPath -Raw -Encoding UTF8

$dashboardUpdate = ConvertFrom-Json -InputObject $dashboardJson
$dashboardUpdate.id = $DashboardId
$dashboardUpdate.timestamp = $null
$dashboardUpdate.hasSolutionTile = $null
$dashboardJson = ConvertTo-Json -InputObject $dashboardUpdate -Depth 100

Write-Host "Posting dashboard update JSON from '$DashboardJsonPath' to '$dashboardUrl'..." -ForegroundColor Cyan

$headers = @{
    Authorization = "Bearer $AccessToken"
}

try {
    $dashboardResponse = Invoke-RestMethod `
        -Uri         $dashboardUrl `
        -Method      POST `
        -ContentType 'application/json; charset=utf-8' `
        -Headers     $headers `
        -Body        $dashboardJson `
        -ErrorAction Stop
}
catch {
    $statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
    $errorBody  = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
    Stop-WithError "Dashboard Update POST failed (HTTP $statusCode). Response: $errorBody`nError: $_"
}

Write-Host "Dashboard $($dashboardResponse.title) updated successfully with $(@($dashboardResponse.workbookItems).Count) tabs!" -ForegroundColor Green

Back to the cookbook