While Migrating from SharePoint 2013 to SharePoint Online, I came across a scenario where I had to replicate the behavior of creating SharePoint Online Sub Site from a template which should include the Site Structure as well as the Flows attached to the template site.
After quite a research, I was finally able to achieve this requirement by following below steps
Getting the SharePoint Template and Flows to be copied ready
- Create a SharePoint Sub Site and the Flows (My Flow was connected to a SharePoint List and had only SharePoint Connections) with the desired structure (From here on will be referred as Source)
- Save the Sub Site as a template using below PnP PowerShell command
- Get-PnPSiteTemplate -Out template.xml (https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpsitetemplate?view=sharepoin...)
- Upload the Template in Documents Library of the Source Site
Create a Flow which will Create new Sub Site based on the template and copy the Flows (Target Site and Target Flows)
- Create a List at the Site Collection level, when an item is added to this list, a new Sub Site will be created with the Flows attached. Lets go ahead with only one column for now as follows
- Title - Will serve as the Title for Sub Site
- Create an Automated Flow, which will trigger when an item is Created in SharePoint List
- Select the Site Address and the List Name from the dropdowns on the "When an item is created" trigger
- Add an action, click on Standard, type in "Power Automate Management" and select the Power Automate Management option
- Type in "Get Flow" and select the respective action
- Select the Environment and the Flow you wish to copy
- Add an action "Get File Content" to get the template file
- Add a Plumsail action "Create a SharePoint Subsite from Template" - this will create the Target Site
- Select the SharePoint Site Collection
- Add a Title for your Target Sub Site
- Template can be specified as 1033 (Teams Site)
- Add the Site URL you wish to generate for the Sub Site
- Add an Initialize Variable action to store the absolute URL of the Target Site as Plumsail action above will return only relative URL
- Add a Plumsail action "Provision PnP Template to SharePoint" to apply our saved template on the Target Site
- Add "Send an HTTP Request to SharePoint" action to get the GUID of the Target List created on the Target Site on which we need to deploy the copied Flow
- Now we have to make some changes in the Flow Definition we are copying, to do this add a Compose Action and then add dynamic content as follows
- Go to Expression, type string and select the string () function
- Inside the string function, add the Dynamic content "Flow Definition" under "Get Flow" category
string(outputs('Get_Flow_1_-Ap')?['body/properties/definition'])
- Add another Compose action to replace the URL pointing to the Source Site with the Target Site's URL
Go to Expression, and add replace function
- Inside the replace function, select the Dynamic content as Output from the previous Compose, add a comma and then insert the Template Site URL and then add a comma and insert the Output from Initialize Variable in step 9
- replace(outputs('Compose-_string'),'https://sample.sharepoint.com/sites/template',variables('SiteUrl'))
- Add a final Compose action to replace the GUID of the Source List with the Target List
- replace(outputs('Compose-_url'),'GUID of Source List' ,outputs('Send_an_HTTP_request_to_SharePoint-AP')?['body']['d']['id'])
- Initialize Array Variable for storing the Connections References of the Source Flow
Add "Append to array action", and Configure it in the following way
- Add an action "Create a Flow" which comes under Power Automate Management, and configure it in the following manner
- Select the Environment
- Provide a name for the Flow
- In Flow Definition, go to expression, add json function and pass the Output of Last added Compose as the parameter
json(outputs('Compose-CA_Id'))
- Select the Flow State as Started
- In the Connection References, add the array we created in step 15
And you are done! You now have replicated your Flow from Source to Target SharePoint Sub Site! 🙂
See it in action for yourself, Post any questions you may have. I will be glad to answer them!