M365 - PowerAutomate - Clone (Create) Flow and Attach it to a Site using a Flow.
Here we will see how to use the PowerAutomate to create a new Flow from a template Flow and attach it to a document library with an ability to maintain the Flow functionality in one place. In this example, we will see how a “Request manager approval for a selected file” template flow can be attached a site and a same mechanism can be applied to any custom Flow.
SharePoint Template Site and Document Library
Provision a new site and a document library for the template “Request manager approval for a selected file” Flow.
Create “Request manager approval for a selected file” Flow
Provision “Request manager approval for a selected file” Flow and attach it to the Document library. This teamplate Flow will be used to create a new instance and attached it to a Site document library.
Create the template Flow and attach to the Document lirary
The flow can be triggered by an user for a selected document.
In this scenario, we will use the above Flow to create a new instance based on this template Flow and attach it to a new Site Document library using an another Flow.
ApprovedSites List
Create a new SharePoint List “ApprovedSites” containing a list of sites which we need to create a new Flow based on the template Flow and attach it to the Document library.
Create a Flow to clone the template Flow and attach it to the Document library
In this step, we will create a new Flow to clone the template flow to a list to clone the template Flow and attach it to the ApprovedSite Document library as below.
Set the Trigger
Attach this flow to the “ApprovedSites”.
This Flow will be triggered everytime a new Approved Site is created in the “ApprovedSites” list.
Initialise the variables
Initialise some variables to hold the template FlowDefinition, Connections values.
Get the “Request manager approval for a selected file” FlowDefinition
Get the FlowDefinition, ConnectionReferences details from the Templated Flow
Set the FlowDefinition, Connections variables with the details from the “GetFlow” action followed by parsing the connections as a JSON and compose the Connections array. These values will be used to create the new flow instance.
Replace the Trigger Url and the Document Library ID in the templated FlowDefinition
The templated Flow contains the Url of the template Site and the Document library and replace it with the ApprovedSite Url and Document Library.
Create Flow for the ApprovedSite Document Library
Call “Create Flow” actions with the FlowDefintion and ConnectionReferences.
The above sets the FlowDefinition and Connection properties with the updated FlowDefinition (containing the ApprovedSite url and DocumentLibrary Id) and Connection References (using the existing connection).
Note: ConnectionReferencesName is set as - variables(‘ConnectionArray’)?[«index»][‘connectionName’] and ConnectionReferencesId is set as - variables(‘ConnectionArray’)?[«index»][‘id’].
ApprovedSites Flow
Once site is registed in the ApprovedSites list, “Create Flow - For Approved Sites” Flow gets executed and creates a Flow on the ApprovedSite based on the templated Flow.
Regiser a Site in the “ApprovedSites” List
The configured flow creates a new Flow instance for the ApprovedSite
The new Flow is attached to the ApprovedSite’s Document Library
Note: The above creates a Flow for a Document Library using the existing connection references and there are certain limits for the PowerAutomate on the number of Flows can be owned by an User or Account etc. More details on the limit and the work around can be found here.