Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable)

Imagine a situation you create a Power Apps button triggered Power Automate Flow. 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 just the flow to 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:

Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable)

Important points: 

  • 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 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:
    Captureaaa
  • By using the ‘Parse JSON’ action, you will be able to easily find the parsed properties among the dynamic content in your flow:dynamic

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:

https://site.sharepoint.com/sites/SPFxDev/_layouts/15/listform.aspx?PageType=4&ListId=9a84d5fe-a73d-4414-beaf-d7c589392468&ID=1&ContentTypeID=0x0100B31E904D1A2B3644A73E46BFCBAE3F4B

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):

Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable)

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

Sample call:

Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable)

This will call the flow, and open the created file (.txt in this case) in a new tab.

Results

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.

Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable)

You could store the ‘output’ property from the Flow in a variable and do other stuff needed.

About the Author:

SharePoint and Office 365 Consultant/Developer always trying to get the best from Microsoft stack (Microsoft 365/Azure/Power Platform and related stuff).

Living in Ireland since 2018, I had previously worked in Brazil as a consultant/developer for Office 365, SharePoint and related technologies for almost 10 years.

This blog is mainly for sharing ideas and solutions using Microsoft technologies.

Follow me on twitter for more updates: https://twitter.com/michelcarlo

Reference:

Mendes, Michel (2020). Power Apps & SharePoint: Dynamically get the site URL from lists used in an App (and make your app Flows easily reusable). Available at: https://michelcarlo.wordpress.com/2020/08/29/create-generic-flows-to-be-called-from-power-apps-and-save-content-in-sharepoint-libraries/ [Accessed: 31st August 2020].

Share this on...

Rate this Post:

Share: