How do I show certain controls on all pages?

There is a set of controls I’d like to show at the top of each page of my dashboard. Is there a way to do that without copy/pasting the code to each file I have that represents a page?

Thanks!

Hello @Hugepickle was thinking on this…so if you read https://docs.universaldashboard.io/components/inputs#replacing-the-contents-of-the-input-card then you only need the one page, which dynamically updates the cards or other components on the page from the “set of tools” you have at the top…or can you explain a bit more of the master plan if this is not what you are after…? Thanks

Thanks, @psDevUK. My main goal is to have some specific text and some input controls, including a dropdown and some others, at the top of each page. I’ve already build my dashboard with multiple pages (separate .ps1 files) and it would be great if I can integrate this into that. Ideally, a way to make that same set of controls appear on each page or somehow create a module with the function I can call for each one that shows the appropriate controls. I’m not sure if it’s possible to use UD controls in a function that is called for multiple pages to have those same controls on each page, but something like that would be great.

You could dot source the file on each page. e.g:
. "$psscriptroot\foldername\filename.ps1"
Granted you’d still need to copy and paste that line to each file but the code to produce your controls will be in one place and can be updated from there.
I suppose it also depends on what controls you’re using and how they’re interacting with the rest of the site.

1 Like

In addition to @insomniacc answer for dot sourcing your code, you could also use the UDEndpointInitialization to add StartupScripts (ps1 files with your code) to each runspace.

In my example i have a subfolder ‘functions’ with all my ps1 files with custom functions that i need for my dashboard on every page.
I am grabbing these files, load them into a variable and add them to the EndpointInitialization.
On my page i do not need to dot source any files or import any function. I only need to run these functions.

You need to create the EndpointInitialization first using New-UDEndpointInitialization for importing a module or a variable to your runspaces. (No Parameter for the StartupScripts class)

So in your case, you can create a function (let’s call these Function Start-PageInitialization) with your UD controls and call Start-PageInitialization on your pages.

Sample Code:

# Each needed function should be saved in the subfolder "functions"
$UD_HelperFunctionsFolder = (Join-Path $PSScriptRoot 'functions')
# Each function 'ps1'-file needs to be imported into each universaldashboard runspace (incl. the current runspace), lets construct a variable with all functions
$colUD_HelperFunctions = @(Get-ChildItem $UD_HelperFunctionsFolder -Recurse -File -Filter *.ps1).foreach( { try { $_.FullName } catch { throw "Function $($_.Name) could not be loaded! Aborting...(Exception: '$($_.exception.message)')" } })
if ($colUD_HelperFunctions)
{
    # Convert the collection to a string, needed for New-UDEndpointInitialization cmdlet (this cmdlet only supports a string array if adding scripts to the runspaces/endpoints)
    [string[]]$strUD_HelperFunctions = $colUD_HelperFunctions
    # Since UniversalDashboard uses different runspaces to execute it's tasks, we need to initialize each runspace with our required modules, functions and scripts
    $EndpointInit = New-UDEndpointInitialization -Module @($Config.Powershell.UD_Module, $Config.Powershell.PSD_Module) -Variable @(Config)
    # Since we are using one function per file in the functions subfolder, we need to import this functions as StartupScripts to each endpoint/runspace
    foreach ($function in $strUD_HelperFunctions)
    {
        # Use the 'Add' Method of the StartupScripts class to add the functions to each endpoint/runspace at startup time
        $null = $EndpointInit.StartupScripts.Add($function)
    }
}
# Since UniversalDashboard needs all required endpoints (Endpoints gathering the data and information) and pages (pages will show the gathered data and information) before starting the dashboard
# So we dot source all of our files in the two subfolders for endpoints and pages. This will load all the files into this function before creating and starting the dashboard
# Using this variable during Start-UDDashboard
$Endpoints = @(Get-ChildItem (Join-Path $PSScriptRoot 'endpoints' ) -Recurse -File).ForEach( { . $_.FullName } )
# Using this variable during New-UDDashboard
$Pages = @(Get-ChildItem (Join-Path $PSScriptRoot 'pages' ) -Recurse -File).ForEach( { . $_.FullName } )
3 Likes

Thanks to both!

Per the mention of calling the functions, is it possible to have something like a module.psm1 with a function like

Function New-MyPageHeader {
New-udinput
New-udinput
etc…
}

and in each page’s .ps1 file, call

import-module module.psm1
New-MyPageHeader

Sure, if you use the New-UDEndpointInitialization you do not need to import the module on each page. Just execute the function New-MyPageHeader. (You could also use Import-Module on every page, of course)

Example:

$UD_HelperFunctions = 'functions\module.psm1'
$Utilities = (Join-Path $PSScriptRoot $UD_HelperFunctions)

# Importing needed functions for UD in each endpoint/runspace
if (Test-Path $Utilities)
{
    # Since UniversalDashboard uses different runspaces to execute it's tasks, we need to initialize each runspace with our required modules and functions
    $EndpointInit = New-UDEndpointInitialization -Module @($Utilities)
}

You need to pass the $EndpointInit Variable to New-UDDashboard

$Dashboard = New-UDDashboard -Pages $Pages -EndpointInitialization $EndpointInit

and then start the dashboard:

$null = Start-UDDashboard -Dashboard $Dashboard -Endpoint $Endpoints -Name "TestDashboard"

And on your pages just call
New-MyPageHeader

1 Like

I love it! Thanks!

1 Like