Query dictionary and Invoke-UDEndpoint OnClick behavior

Product: PowerShell Universal
Version: 4.2.7

Greetings,

I’m working on an app to pull information from various APIs for a single workstation. I want to be able to pass the workstation name into the UD app via URL using the "?workstationname=WORKSTATIONNAME" query. If the query was used I’d like it to populate the textbox with the machine name that was passed in and automatically execute the contents of the button’s -OnClick scriptblock. I’d also like the page to be able to be used manually where a user would enter the workstation name, then click the button and get the same information as if there had been a device name passed in via the query.

I’m seeing some pretty strange behavior from how I’ve implemented, though, and am at a bit of a loss. I’ve tried multiple rewrites and different placements and organizations of the various pieces of logic that do what I want to no avail.

The first code block below seems to work the first time the page is deployed (when I click save in the PSU app “Code” window) but does not work subsequent times until it’s redeployed again. On subsequent runs the $workstationName variable is null and isn’t passed into the body of the code.

Also of note; this doesn’t seem to work at all if I splat the New-UDApp params (the second code block), which would be my preferred method of creating my apps for the sake of clarity and future management. The $workstationName variable is always null.

Both of these work as expected when the button is clicked, both in the stripped-down code below and my actual code.

Am I missing knowledge on something to do with the session or page variable scopes? I’m relatively new to PSU and don’t quite understand the idea of using different pages or endpoints in the scope of PSU yet.

https://FQDN/stuff?workstationname=WORKSTATIONNAME

Works when saved/redeployed the first time, then does not work subsequent times.

New-UDApp -Title "Workstation Info" -Content {
    New-UDLayout -Columns 5 -Content {
        New-UDPaper -Id "inputPaper" -Content {
            New-UDElement -Tag "span" -Content {
                New-UDStack -Direction Row -Spacing 2 -Content {
                    If (-not [String]::IsNullOrEmpty($Query["workstationname"])) {
                        New-UDTextbox -Id "workstationTextbox" -Label "Workstation" -Value $Query["workstationname"]
                    }
                    Else {
                        New-UDTextbox -Id "workstationTextbox" -Label "Workstation" -Placeholder "5CG123XXX" -Autofocus
                    }
                    New-UDButton -Id "buttonStart" -Style $buttonStyleStart -Text "GO" -ShowLoading -OnClick {
                        $workstationName = (Get-UDElement -Id "workstationTextbox").Value
                        Show-UDToast -Message $workstationName

                        # Lots of other stuff
                    }
                }
                If(-not [String]::IsNullOrEmpty($Query["workstationname"])) {
                    Invoke-UDEndpoint -Id "buttonStart" -Session
                }
            }
        }
    }
}

Never works:

$newUdAppParams = @{
    Theme                 = @{
        palette = @{
            background = @{
                default = "#ADADAD"
            }
        }
    }
    Title                 = "Workstation Info"
    Logo                  = ""
    HeaderContent         = { New-UDButton -Icon (New-UDIcon -Icon "Home") -Text "Home" -Style @{background = "#1A3A6A" } -OnClick { Invoke-UDRedirect -Native "/home" } }
    HeaderBackgroundColor = "#FFFFFF"
    HeaderColor           = "#000000"
    DisableThemeToggle    = $true
    Content               = {
        New-UDLayout -Columns 5 -Content {
            New-UDPaper -Id "inputPaper" -Content {
                New-UDElement -Tag "span" -Content {
                    New-UDStack -Direction Row -Spacing 2 -Content {
                        If (-not [String]::IsNullOrEmpty($Query["workstationname"])) {
                            New-UDTextbox -Id "workstationTextbox" -Label "Workstation" -Value $Query["workstationname"]
                        }
                        Else {
                            New-UDTextbox -Id "workstationTextbox" -Label "Workstation" -Placeholder "5CG123XXX" -Autofocus
                        }
                        New-UDButton -Id "buttonStart" -Style $buttonStyleStart -Text "GO" -ShowLoading -OnClick {
                            $workstationName = (Get-UDElement -Id "workstationTextbox").Value
                            Show-UDToast -Message $workstationName

                            # Lots of other stuff
                        }
                    }
                    If(-not [String]::IsNullOrEmpty($Query["workstationname"])) {
                        Invoke-UDEndpoint -Id "buttonStart" -Session
                    }
                }
            }
        }
    }
}

New-UDApp @newUdAppParams

Doing a bit of thread resurrection - But I am stumbling on a very similar issue and can’t figure it out.

One app, two pages, page1Searcher and page2Searcher, separate ps1 files.

page1Searcher works - if I click the button or enter it copies values to box2
if I have something in the querystring it fills the value in box1

New-UDPage -Url 'page1Searcher' -Content {
    New-UDTextbox -Autofocus -Id 'box1' -Label 'box 1' -Value $Query['UserName'] -OnEnter {
        Invoke-UDEndpoint -Id 'CB'
    }

    New-UDButton -Id 'CB' -Text 'Click' -OnClick {
        $v = Get-UDElement -Id 'box1' -Property Value
        Set-UDElement -Id 'box2' -Properties @{Value = $v }
    }
    
    New-UDTextbox -Id 'box2'
} -AutoInclude

page2Searcher redirects as expected, but Invoke-UDEndpoint does not.

New-UDPage -Url 'page2Searcher' -Content {
    New-UDLink -Url "/page1Searcher?UserName=username" -OnClick {
        Invoke-UDRedirect -Url "/page1Searcher?UserName=username"
        Invoke-UDEndpoint -Id 'CB'
    }
} -AutoInclude

If I have visited page1 first, the redirect redirects, fills in the querystring value, but doesn’t click the button. If I have not visited page1 first I get an error Endpoint CB does not exist

I have tried adding various if statements and Invoke-UDEndpoints, and can not figure out a way to consistently make it behave.

So, @Eszrah_ap_Niht did you ever figure this out? Or does someone else have a good idea? It feels like I am missing the obvious and super simple answer…