Triggering app dynamic regions from scripts or API endpoints

Hey,
I used to be able to do something similar waaay back on an earlier version of UD with the Sync-UDElement function, but i’m struggling currently on 4.0.9 trying to work this one out.

Right now I’ve got a new-uddynamic on my home page:

New-UDDynamic -Id "TestDynamic" -Content {
    Show-UDToast -Message "Test Message" -Duration 9000
}   

I’m trying to work out how I can trigger this from an API endpoint or script.
I’ve tried using Sync-UDElement but it doesnt have the -integrated flag (this is the way I used to do it before but back when it was only app tokens).
Using Sync-UDElement -Id "TestDynamic" -Broadcast throws:
[error] You cannot call a method on a null-valued expression.

So instead I’m now trying:

Sync-PSUComponent -Integrated -Id "TestDynamic" -Broadcast

and it’s also throwing
[error] Specified method is not supported.

Anyone else get this working?

Okay I figured out how to get it working, but not sure if this is a bug or not.
My script was running on the same custom environment as my App where the component is, when i changed the script to run on the integrated environment instead, this started working.

Seem to have difficulty getting Set-PSUCache to work too.
Let me give a litte more context to what I’m trying to acheive here.

Basically I have a nightly web app restart at 2am.
I doubt i’ll many users visiting at this time, but just in case I was thinking of putting new-uddynamic at the top of all my pages which contains an if statement, based on a cache variable, it triggers a toast or modal or something advising the site will go offline for maintenance in 5 - 10mins or something to that effect.

This is what I have at the top of my pages:

New-UDDynamic -Id "AppMaintenancePlaceholder" -Content {
                if($Cache:MaintenanceAlert -eq $true){
                    Show-UDToast -Message "Test Maintenance Message: $Cache:MaintenanceAlert" -Duration 9000 -BackgroundColor Red -Broadcast
                }
            }

I’m trying to trigger this from a script. Here’s my script:

Set-PSUCache -Integrated -Key "MaintenanceAlert" -Value $true
Sync-PSUComponent -Integrated -Id "AppMaintenancePlaceholder" -Broadcast

The Sync command triggers the dynamic, but the if statement isnt triggering because the cache variable isnt updating and is always empty.

My initial thought before cache got involved was just to have an empty new-uddynamic that i could just set the content of and trigger, but set-uddynamic cant be done from an api/script from what I can tell?

Just tried in its most basic form, this is on my page:

New-UDDynamic -content {
    New-UDHTML -markup "cache: $Cache:TestCache"
} -AutoRefresh -AutoRefreshInterval 1

and this is in my script:

Set-PSUCache -Integrated -Key TestCache -Value "true"

When I run the script the cache does not update.

Figured I’d try the same using a Variable instead of cache.
I manually created a bool variable called TestVar and set it to $false in the gui.

In my script, attempting to use

Set-PSUVariable -Variable "TestVar" -Value $true -Integrated 

returns [error] Variable not found

and

Set-PSUVariable -Name "TestVar" -Value $true -Integrated

returns

[error] Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided. 

Finally got it working but had to do it this way:

$Var = Get-PSUVariable -Name "TestVar" -Integrated
Set-PSUVariable -Id $var.ID -Value $true -Integrated

I’ve also noticed another thing here, whenever I update a variable’s value in PSU (either by gui or command) any configured Apps/dashboards restart and show as ‘Deploying’, which is not ideal.

$Cache and Set-PSUCache are actually different caches.

You need to use Get-PSUCache to get the value set by Set-PSUCache. As I’m typing this, it makes me realize this is weird and we should probably improve it.

The variables resetting apps is by design as we set the variables when the app starts. That said, this should also be improved and has been on my list for awhile. You can avoid this by turning off auto-deploy on any app you dont want this to happen.

I’ll have to look at sync-psucomponent. At first glance, it seems like all this should be implemented properly but I need to test.

1 Like

Thanks man! This is all in it’s infancy so we’re just using it internally at the moment so there’s no real rush or impact from these things (not to mention I’m leaving the org in a week anyway). Appreciate the tip on auto deploy!
I’ll be continuing my work on my personal instance in the mean time and hope to introduce PSU in the new org I join too.