Blog

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.

It can be very useful to be able to retrieve the session ID from a running app for troubleshooting or diagnostic purposes. For example, where there are intermittent platform issues that could crash an app, the ability to retrieve a session ID before a problem occurs can assist in any subsequent conversations with Microsoft support.

Ideally, it would be great to be able to retrieve the session ID from App signal, as suggested by Eric Thomas in his idea post here:

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Provide-a-SessionID-Property-to-Automate-Trouble-Reports/idi-p/278620

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:

triggerOutputs()?['headers']?['x-ms-client-session-id']


In the final step of our flow, we add a 'Respond to a PowerApp flow' action to return the variable that contains the session id.

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.

Set(varSessionID,GetPowerAppsSessionID.Run())
The screenshot beneath shows how to attach this formula to the OnSelect property of a button. We could equally attach this to the OnVisible property of a screen, or the OnStart property of an app.


Verifying that the technqiue works

The screenshot beneath shows the label that I added to display the return value from the flow.


As we can see, this corresponds to the session ID that is reported in the File > Account menu section of the designer.

Conclusion

There's no direct way to retrieve the session ID from within a running Power App. A workaround is to retrieve the session ID from the trigger body of a flow, and to return it to Power Apps through a 'Respond to a PowerApp flow' action.
  •   Categories: 
  • flow