Pass in query string parameter

anyone know of a way to retrieve the current url and pass it into a dashboard?

trying to host a dashboard in SharePoint and when the user clicks on the link to launch the dashboard I am passing in a query string value. How would the dashboard grab this query string value?

for example, a user clicks on the following link http://localhost:10001?location=usa

how would the dashboard pull out “usa” in the above link so it can run custom logic based on that parameter?

I have tried to use “[System.Web.HttpContext]::Current.Request.RawUrl” but nothing is being returned. any help is appreciated!

On the destination page $request.Headers["referer"] will give the url of the link that sent you there.
$request.Headers["referer"].split("=")[1] give you usa.
There’s probably a better way in case of multiple parameters…

gav

If you had more parameters eg “http://localhost:10001?location=usa&state=texas
You could substring using indexof() to pull out individual values. More flexible that just splitting.

Might even be simpler to skip the key value query parameters and go for simple values eg
http://localhost:10001#usa/texas

gav

thanks, that should pull out the query string! I think the question I really have is how do I pass the url and receive it in the dashboard? that $request object is null when I try this:

Dashboard = New-UDDashboard -Title "Hello, World!" -Content { New-UDHeading -Text "url is: '($request.Headers[“referer”])’" -Size 1; }

You’ll need to ensure that you are calling this from an endpoint. Content is executed when you start the dashboard. Endpoint is executed when the user visits the page.

Dashboard = New-UDDashboard -Title "Hello, World!" -Content { 
   New-UDElement -Tag 'div' -Endpoint {
      New-UDHeading -Text "url is: '($request.Headers[“referer”])’" -Size 1; 
   }
}

ahhh, the -Endpoint was the piece I was missing. thank you!

for completeness, here is a sample code that works for what I need. hope it helps someone else!

$Dashboard = New-UDDashboard -Title "Hello, World!" -Content { 
   New-UDElement -Tag 'div' -Endpoint {
        $queryStringValue = "location"
        $url = $($request.Headers["referer"]);
        $queryStringIndex = $url.IndexOf('?')

        # no query strings were passed in
        if($queryStringIndex -eq -1) {
            New-UDHeading -Text "No query string passed in" -Size 1
        }
        elseif ($queryStringIndex -ge 0) {
            $queryString = $url.Substring($queryStringIndex);
            [System.Collections.Specialized.NameValueCollection]$queryStringCollection = [System.Web.HttpUtility]::ParseQueryString($querystring);
            New-UDHeading -Text "$queryStringValue = $($queryStringCollection[$queryStringValue])" -Size 1
        }
   }
}
2 Likes

@adam, should this still work in UD 3.1.1? $request is always empty and I am looking for a way to get the query string parameters.

Currently not possible but I’ll get this into 1.5. It’ll behave like APIs where if you pass query string values, you will have access to them as variables in the dashboard.

EDIT: This will be in tonight’s nightly: https://docs.ironmansoftware.com/dashboard/components/pages#query-string-parameters

1 Like

Works like a charm. I want to separate a project into multiple dashboards for maintenance and being able to pass data to the next dashboard really helps. Thanks for adding this!

1 Like

Does this still work in version 4.2.1?
I can’t get variable inside de script when using ?variable=value

As of version 4.0.10, Query string values are now passed in via a $Query dictionary rather than as variables to avoid potential injection issues

So for a URL that’s like ?variable=value, you access the value by using $Query['variable']

1 Like