Using PowerAutomate to deploy PowerAutomate (Flow deploying Flow)… Sounds like Inception?

Cobb: “Once an idea has taken hold of the brain it’s almost impossible to eradicate. An idea that is fully formed – fully understood – that sticks; right in there somewhere.”

I’ve not been using Power Automate heavily in recent projects, just the odd small workflow here and there, but just recently a colleague asked me if it was possible to automate Flow Deployment.. i.e. deploy a flow with a flow?

Not being 100%, I reached out to a few friends in the User Group community and Serge Luca came back very quickly and said, “Have you looked at the Create Flow” action in the Flow Management Connector.

On the face of it, the “Create Flow” action looks very simple to use, you select your environment, give it a display name, drop in a definition and give it a state of On or Off. Simples!

clip_image002

Well of course nothing is that simple and in this case it’s the Flow Definition that will keep you awake. On the face of it, the definition is fairly simple JSON that complies roughly to the following:

clip_image004

To test this out, I used the “Get Flow” action to get a valid definition of a flow attached to a SharePoint Library, then took the output of this and piped it directly into the “Create Flow”. This however didn’t work until I realised that I needed to include the Connection References from the received flow too.

clip_image006

Note: Clicking on the icon to the right of the references collection drops it down to a single box for a collection reference.

With all the elements in place, the flow runs perfectly, creating a new copy of the flow attached to the same SharePoint list. Ok great.. we’re now getting two email alerts instead of one!

The real power in this scenario now, would be to take the source Flow and to re-attach the trigger connection to a new library in a new site collection. The Scenario for this is that we’re using a mixture of Site Designs and PNP to create a particular type of site and we need to include a flow on each site that is built. As flows currently do not get included in site designs, the only option is to try and do this through automation, by calling the flow after the PNP work is finished.

To test this theory, I took the output of the Flow Definition step in our test email which is JSON format text and edited it in VS Code. Looking at the structure of the workflow, the only thing I need to change is a few elements in the trigger. These are:

/inputs/path

/inputs/queries/folderpath

I updated this with the correct new values and then pasted the whole JSON back into the “Create Flow” action, only to be met with a Big red dot on the flow checker. Looking at the error it reads “Correct to include a valid reference to ‘Get_my_profile_(V2)’ for the input parameter(s) of action ‘Create_Flow’”

But I’m not using the Get Profile action in this flow? However I am using it in the source flow that we’re copying. On Examination when we look at what’s been pasted into the “Create Flow” action, we can see that the app designer has parsed all of the text, including the parameters of the actions embedded in the workflow definition.

clip_image008

This had me at a bit of a loss and I toyed with the problem for a day or two before posting on the Power Platform forums and reaching out to a few people in the community. Luckily, John Liu came to my rescue and pointed to some fun he had using Geolocation for Pokemon Go! (And I love this example of using flow for fun!, click on the Tweet below to see the full trail.)

The pointers he gave me in that tweet showed me that I needed to break up and rebuild the Flow Definition. I didn’t quite realise how granular I needed to be though and in the end we pretty much rebuilt the definition dynamically throughout using a lot of Compose Actions.

As the flow will eventually be triggered by an HTML request, I started building using a Manual Trigger, capturing four key pieces of information.

· siteURL – The URL of the site that has been created

· flowName – The new name of the dynamically created flow.

· listName – The name of the list

· folderPath – Usually the name of the list (unless it’s the Documents list, in which case it’s “Shared Documents”

clip_image010

Note: Notice the Parameter names that we list in the Trigger action above? These are NOT the names that you find them by when addressing the JSON in the step named “Initialise SiteURL Encoded” in the next step. Instead they are labelled “text”, “text_1” etc internally! If you Peek the code you can see what I mean. This isn’t a problem when using the Dynamic data selector, only when using the TriggerBody() function.

Since I demo’d this issue as the London PPUG, Pieter Veenstra went away to find a way to correct this issue. It does mean exporting and re-importing the flow, but it does resolve the naming problem!

Read Pieters blog post from this link.

After this, we have some setting up to do. First we get all the lists and libraries from the site (I realised after I wrote this, that this step is NOT needed as we can just call for the library ID using an HTTP request!) and also capture the Site URL as an encodedURIComponent so that we can safely embed it in the JSON.

clip_image012

Then we get the target List from a SharePoint Rest call so that we can use the ID GUID from the list in our definition (this is also URL Encoded as per the default definition from Flow).

clip_image014

Following this, the Get Flow action allows us to grab a copy of our Source flow. For our scenario, this flow is permanently attached to our Template site which is kept to allow for updates to the Site Design.

clip_image015

It’s at this point where things go a little crazy. During the reverse engineering that I did, I found that using a single compose action wasn’t suitable as I kept getting crazy errors like: “The flow cannot contain unsupported parameter function expressions” which I traced back to some of the parts of the definition that I wasn’t intending to change.

clip_image017

To get around this, I had to break out the individual elements of the JSON definition into single Compose Actions, then bring them all together at the end into a new block of JSON. There might be more efficient ways of doing this using SetProperty like John does in his Flow, but I just haven’t had as much exposure to the Expression functions yet and this worked for me!

clip_image018

The first two actions use the same method to grab the relevant portions of the JSON from the “Get Flow” action. In the Inputs field, we use the expression “body(‘Get_Flow’)[‘properties’][‘definition’][‘parameters’] to grab the block of data that we want.

clip_image019

As we’re not making any changes to Parameters or Actions at this point, we don’t need to be any more granular than that. These blocks will just be added back into our Flow Definition at the bottom of this step.

The Triggers section is where it becomes a little more difficult, so we start by building up the sections that we do want to amend, Path and Query.

clip_image020

For the path, we inject our URL encoded Site URL and the ListID into a new string that replaces this line from the original definition:

“path”: “/datasets/https%253A%252F%252F<tenant>.sharepoint.com%252Fsites%252FTestSiteForPen/tables/ecdd0b51-6f77-42fb-8aad-63e2312a95fe/onnewfileitems”

clip_image021

For the query, we inject the folder path (collected at the start) into a JSON object string.

clip_image022

Then finally we grab the Host and Authentication elements in their entirety as we’re not making any changes to these either.

With all of these elements in place, we can now build up the new trigger element as a new block of JSON, injecting the relevant elements as needed (Note the “Outputs” come from the relevant Compose action).

clip_image024

And then finally, we create a new Flow Definition bringing together all of the elements:

clip_image025

And then the finished “Create Flow” action

clip_image026

With this saved, I created a new site collection, then called the flow, passing in the URL, library name and the other required information. This created the Flow which duly triggered when a file was uploaded into the library.

Then I had a drink to celebrate! 😎

9 comments

1 ping

Skip to comment form

    • Nigel Price on Wed 12 Feb 20 at 8:45 pm
    • Reply

    Eeeek !
    What palaver !
    I hope the PowerAutomate team will sort this mess out sometime soon.

    1. John Lui was keen to point out that his FlowStudioApp does resolve these issues.. but it is an additional purchase.

  1. I was able to work around this error by using a more primitive method:

    – I used Get Flow to pull in my ‘template’ flow
    – Used a Compose action to convert the JSON definition to a string
    – Used a few Compose actions with find & replace expressions to plug in my new site URL and list name
    – Used a Compose action to convert the string back into JSON
    – Plugged the output of this Compose action to the Flow Definition field of the Create Flow connector

    • Raf on Wed 21 Oct 20 at 5:25 am
    • Reply

    For some reason I keep getting a the API operation does not allow writing a value for parameter ‘Flow/properties/connectionReferences[0]/displayName’. This parameter is read only.

    Any tips?

    1. That’s weird as we’re not doing anything to the ConnectionReferences, you should just be passing them straight through to the Create Flow step intact from the Get Flow.. I’ll re-run my test flow and see if anything has changed.

        • Stephen on Mon 28 Dec 20 at 7:09 am
        • Reply

        I am getting the same error here too. It says “The ‘inputs.parameters’ of workflow operation ‘Create_Flow’ of type ‘OpenApiConnection’ is not valid. Error details: The API operation does not allow writing a value for parameter ‘Flow/properties/connectionReferences[0]/displayName’. This parameter is read only.”. I came across this forum message (https://powerusers.microsoft.com/t5/Using-Flows/MS-Flow-Failed-to-Create-Flow-using-Flow-management-quot-Create/td-p/718197). When I tried it I got “The request content was invalid and could not be deserialized”.

    • Yvett on Fri 12 Feb 21 at 11:54 am
    • Reply

    I’ve followed this tutorial and everything works well till i call the “Create Flow” action. I got a “Bad Request” and “This is a new issue. ” errors. I have uploaded my problem on here for more information:
    https://powerusers.microsoft.com/t5/Building-Flows/Create-flow-BadRequest-error/m-p/824946

    Anyone got any idea what’s going wrong? I appreciate any and all help. 🙂

  2. Thanks Yvette, I’ve responded on the forum.

  1. […] yesterday’s user group meeting Paul Hunt mentioned an issue with inputs in the manually trigger a flow […]

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.