Imagine a situation where you create a Power Automate Flow triggered by a Power Apps button. This Flow stores a file in SharePoint from data in the app and you need to use the same Power Automate Flow idea in different Power Apps canvas apps. It’s basically the same Flow to just export content from the app to a file in a SharePoint.
Would you recreate the same flow and edit the copies over and over again if needed, just to point to different SharePoint sites?
The best thing would be to make a generic Power Automate Flow and reuse the same Flow, and just plug it into a Flow button inside of other Power Apps.
Here’s a quick sample of a Flow like that, the idea is, Power Apps sends the data to the Flow, we create a file, and respond to the Flow call with the file URL:
- File name and Site URLs in SharePoint actions in the Flow are dynamic based on parameters passed to the app, so control over names, extension and where to store the file can be done differently in each Power App
- The content sent from the Power App will be in JSON format and is sent using a single parameter, formatted as a string to simplify the process. If more parameters need to be added to the flow in the future, this makes the process of updating it simpler:
- By using the ‘Parse JSON’ action, you will be able to easily find the parsed properties among the dynamic content in your flow:
We could ‘hardcode’ some parameters inside of the apps, but let’s say a scenario where you need to reuse the same Canvas App source in different sites, even though we have to save copies of the app re-add the connections manually when publishing the app, it would be a better practice not to be changing code when pointing the Power App to the copy of the source list in a different site.
Choose a list from your app where you are sure there’s going to be any content
For this trick to work, we need to have a list in the app that we are sure there is any content.
Given an example that in several places in the app we use Ireland counties, which are stored in a SharePoint list. For performance improvement, they are copied locally to a collection so a single server call is done across all the screens (this is an optional step, you could access the item directly in the list and store the site URL in a variable for later use anyway).
Based on this collection, or in a list if you prefer, we then just get the first item, and parse the ‘Link to item’ property, which will be something like:
Using the Split function, we split the string in an array based on the “/layouts” token, and return only the First item of the array, which is the site part of the string as follows (you can manage to use this on AppStart or in the OnVisible event of a screen):
Passing values to the Flow
Now let’s say with a flow like the one above:
- First, you add it to the app to be called from a button
- Then even if it has one string parameter only, use the JSON function in Power Apps to convert your object content to a JSON string that can be parsed in the flow
This will call the flow, and open the created file (.txt in this case) in a new tab.
Below an example of a sample app implementing this process. Content for the file and file name are coming from the two text boxes, and the button ‘Save to SharePoint’ calls the Flow as mentioned above.
You could store the ‘output’ property from the Flow in a variable and do other stuff if needed.
I just wanted to show a quick sample of making the flow generic and opening the file result as shown above. Do you make your Power Apps / SharePoint Flows more generic in different ways? Let me know in the comments!