How to retrieve and display the session ID from inside a running Power App
There's no direct way to retrieve the session ID from within a running Power App. A workaround to this issue is to retrieve the session ID by calling a Flow. This post walks through how to implement this technique.
In the meantime, a workaround is to retrieve the session ID by calling a Flow. When we call a flow from Power Apps, we can retrieve a wide range of details from the trigger body, including the session ID. We can then pass this value back to Power Apps through a 'Respond to a Power App' action. Here are the steps to implement this technique.
What's the Trigger Body in Flow?
When we call a Flow from Power Apps, Power Apps triggers the flow through a web request. Like every web request, this includes a HTTP header and body. The trigger body effectively returns the HTTP body of the call from Power Apps.
To demonstrate, the screenshot beneath shows the result from a flow run history where I stored the trigger body content into a variable.
When we examine the content of the trigger body, we see that it includes a wide range of meta data. This includes the content that we typically expect to see in an HTTP request, including the referrer details, and the browser agent. The interesting value here is 'x-ms-client-session-id' - this is the key value that corresponds to the session ID.
Creating a Flow that retrieves the Session ID
Now that we understand exactly where the session id appears in the trigger body, we can construct a flow to retrieve the value.
First, we create an 'Instant cloud flow' with a PowerApps trigger. Next, we can retrieve the session ID into a string variable by addin an 'initialize variable' action, and setting the value to the following expression:
Calling the flow to retrieve the Session ID from Power Apps
From Power Apps we can now call our flow to retrieve the session ID.
From the Action > Power Automate menu item, we add our flow. Assuming that the name of our flow is GetPowerAppsSessionID, we can use the following formula to call the flow and to store the session ID in a Power Apps variable called varSessionID.
Verifying that the technqiue worksThe screenshot beneath shows the label that I added to display the return value from the flow.