New-UDInputField -Type 'select': Values not refreshing

Hello community,

I recently created a Universal Dashboard page and want a dropdown list with values. For that I used the following command:

New-UDInputField -Type ‘select’ -Name ‘CRCOM_Componenttype’ -Placeholder ‘Componenttype’ -Values (Invoke-Command -ScriptBlock {Here I provided a script, which gets values from a SQL Table})

This is working so far, but unfortunately the values in the dropdown will not be updated, if I update them in the SQL table, the script is selecting from.
So it seems that it Caches the values initially and not update them.
If I restart the Dashboard completely, then it updates the values, but this is no unsustainable solution

Anyone a idea how to solve this Problem?

Hello @roobox and a big welcome to the UD forums…what you need to do is to use New-UDColumn with and -Id parameter and an ENDPOINT script block…nest your code in the ENDPOINT to populate the dropdown list…if this is pulling directly from the database, then you can use the AUTOREFRESH and REFRESHINTERVAL parameters to automatically update the dropdown…or you could create an UPDATE button which would have a SYNC command to refresh the dropdown component. I hope this answers your question and doesn’t give you a headache :smiley: if you post your code on here, sure myself or someone else can tweak it for you, or if this is enough info then go go go…This might also help a blog I wrote:- https://psdevuk.github.io/ud-flix/Content-vs-Endpoint/

1 Like

Hello psDevUK,

thank you for the quick answer.
I did some testings regarding your description, but it didn´t work as expected.

Here the relevant snippet of my code:

New-UDColumn -Size 8 {
New-UDInput -Title ‘Create Component’ -BackgroundColor ‘#252525’ -FontColor ‘#FFFFFF’ -Content {
New-UDInputField -Type ‘select’ -Name ‘CRCOM_Componenttype’ -Placeholder ‘Componenttype’ -Values (Invoke-Command $get_all_componenttypes)
New-UDInputField -Type ‘select’ -Name ‘CRCOM_Failurefactor’ -Placeholder ‘Failurefactor’ -Values (Invoke-Command -ScriptBlock {0…100})
New-UDInputField -Type ‘select’ -Name ‘CRCOM_Line’ -Placeholder ‘Line’ -Values (Invoke-Command $get_all_lines)
New-UDInputField -Type ‘checkbox’ -Name ‘CRCOM_Active’ -Placeholder ‘Visible’
} -Endpoint {
param($CRCOM_Componenttype, $CRCOM_Failurefactor, $CRCOM_Line, $CRCOM_Active)
if ($CRCOM_Componenttype -and $CRCOM_Failurefactor -and $CRCOM_Active -and $CRCOM_Line)
{
Invoke-Command $create_component -ArgumentList $CRCOM_Componenttype, $CRCOM_Failurefactor, $CRCOM_Active, $CRCOM_Line | Out-Null
New-UDInputAction -Content @(
New-UDCard -Title “Component successfully created” -BackgroundColor “#0fba00” -FontColor “#ffffff” -Content{
New-UDParagraph -Text “Componenttype: $CRCOM_Componenttype” -Color “#ffffff
New-UDParagraph -Text “Failurefactor: $CRCOM_Failurefactor” -Color “#ffffff
New-UDParagraph -Text “Visibility: $CRCOM_Active” -Color “#ffffff
New-UDParagraph -Text “Line: $CRCOM_Line” -Color “#ffffff
} -Links @(
New-UDLink -Text ‘Create another component’ -Url “http://test.test.net:80/$sitename_long” -FontColor ‘#ffffff’ -Icon check_square_o
) -Size ‘small’
)
}
else
{
New-UDInputAction -Content @(
New-UDCard -Title “Please select a values from the dropdowns” -BackgroundColor “#c40000” -FontColor “#ffffff” -Content{
New-UDParagraph -Text “We changed nothing” -Color “#ffffff
} -Links @(
New-UDLink -Text ‘Retry’ -Url “http://test.test.net:80/$sitename_long” -FontColor ‘#ffffff’ -Icon check_square_o
) -Size ‘small’
)
}
}
}

Hey @roobox thanks for chucking your code on here…brain still feeling scrambled from work today…however I thought I could build a dashboard that would be generic enough for anyone to hopefully use and understand…so from your question was basically how do you dynamically update a drop-down list…so here is my spin on it, which loads all processes on the machine, then updates every 10 seconds and will show any newly loaded processes in the select list…I believe this is what you wanted, so this is what I coded…

Import-Module -Name UniversalDashboard.Community -RequiredVersion 2.8.1
Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Port 1000 -Dashboard (
    New-UDDashboard -Title "Powershell UniversalDashboard" -Content {
        New-UDRow -Columns {
            New-UDColumn -id procs -Size 6 -Endpoint {
                New-UDSelect -Label "Processes" -Option {
                    Get-Process | % { New-UDSelectOption -Name "$($_.Name)" -Value "$($_.Id)" }
                } -OnChange {
                    Show-UDToast -Message $EventData
                }
            } -AutoRefresh -RefreshInterval 10
        }

    }
)

I hope this helps you out. Peace

1 Like

Hello psDevUK,

I used your code and tried to access a variable in the endpoint sections, which is definied outside.
This throws an error, as endpoint opens a new runspace and so the definied variable is “out of scope”.

I tried using the “Cache:” declaration or the “EndpointInitialization” cmdlet, but with none of them is working, so I can not access the variable inside the endpoint block.

Do you know how to make this work?

I have another Question:
In your provided link there is the following sentence in the “Content” block:
If you did put a dynamic element in the content script block such as get-date then this would only run that command everytime you press the F5 key or refresh symbol on the browser you are using.

I think such a “dynamic element” would also work for my code. How can I declare in the Content script block a “dynamic element”, which gets updated with every F5?

Hi @roobox thinking :thinking: have you read https://docs.universaldashboard.io/endpoints/custom-variable-scopes this should help you out.
Again if you can post your code that would help. Thanks

Hello psDevUK,

I did some more testings regarding the “endpoint” script block and finally i got it working!
Thanks you so much :slight_smile:

1 Like