.NET  

Programmatically Manage Power Automate Flows: How to Create, Modify, and Remove Cloud Flows with Code

Power Automate helps automate tasks and processes through cloud flows, but managing those flows at scale can become a challenge. If you're building internal tools, managing environments, or automating deployments, you can take control by creating, updating, and deleting flows programmatically using the Dataverse Web API or the .NET SDK.

Create a new .NET console app project. For this project, we are using Visual Studio 2022 and targeting .NET 6. Steps are given in my previous article.

In this article, we will see,

  • Create new Power Automate flows via code
  • Update existing cloud flows (name, description, logic)
  • Delete cloud flows programmatically

Create a cloud flow

  • The required properties for automated, instant, and scheduled flows are: category, name, type, primaryentity, and clientdata. Use none for the primaryentity for these types of flows.
  • The state code of all flows created this way is set to 0 (Draft or 'Off'). The flow needs to be enabled before it can be used.
  • The most important property is the clientdata, which contains the connectionReferences that the flow uses, and the definition of the flow. The connectionReferences are the mappings to each connection that the flow uses.
/// <summary>
/// Creates a cloud flow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <returns>The workflowid</returns>
public static Guid CreateCloudFlow(IOrganizationService service)
{
   var workflow = new Entity("workflow")
   {
         Attributes = {
            {"category", new OptionSetValue(5) }, // Cloud flow
            {"name", "Sample flow"},
            {"type", new OptionSetValue(1) }, //Definition
            {"description", "This flow reads some data from Dataverse." },
            {"primaryentity", "none" },
            {"clientdata", "{\"properties\":{\"connectionReferences\":{\"shared_commondataserviceforapps\":{\"impersonation\":{},\"runtimeSource\":\"embedded\",\"connection\":{\"name\":\"shared-commondataser-114efb88-a991-40c7-b75f-2693-b1ca6a0c\",\"connectionReferenceLogicalName\":\"crdcb_sharedcommondataserviceforapps_109ea\"},\"api\":{\"name\":\"shared_commondataserviceforapps\"}}},\"definition\":{\"$schema\":\"https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#\",\"contentVersion\":\"1.0.0.0\",\"parameters\":{\"$connections\":{\"defaultValue\":{},\"type\":\"Object\"},\"$authentication\":{\"defaultValue\":{},\"type\":\"SecureObject\"}},\"triggers\":{\"manual\":{\"metadata\":{\"operationMetadataId\":\"76f87a86-89b3-48b4-92a2-1b74539894a6\"},\"type\":\"Request\",\"kind\":\"Button\",\"inputs\":{\"schema\":{\"type\":\"object\",\"properties\":{},\"required\":[]}}}},\"actions\":{\"List_rows\":{\"runAfter\":{},\"metadata\":{\"operationMetadataId\":\"9725b30f-4a8e-4695-b6fd-9a4985808809\"},\"type\":\"OpenApiConnection\",\"inputs\":{\"host\":{\"apiId\":\"/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps\",\"connectionName\":\"shared_commondataserviceforapps\",\"operationId\":\"ListRecords\"},\"parameters\":{\"entityName\":\"accounts\",\"$select\":\"name\",\"$top\":1},\"authentication\":\"@parameters('$authentication')\"}}}}},\"schemaVersion\":\"1.0.0.0\"}" }
         }
   };

   return service.Create(workflow);
}

It will return an ID of the newly created cloud flow, you can check the flow in your environment.

Cloud flow

Update a cloud flow

To update a flow, set only the properties you want to change.

  • 1. The flow is turned off; we will turn it on.
  • 2. We will change the description of this flow.

First, edit the flow.

Edit flow

Second, change the connection reference.

List row

Third, publish the flow to update it.

Update

This static method requires an authenticated client to implement the IOrganizationService. It uses the IOrganizationService. Update method to update a flow description and set the owner.

/// <summary>
/// Updates a cloud flow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="workflowid">The ID of the flow to update.</param>
/// <param name="systemuserid">The id of the user to assign the flow to.</param>

public static void UpdateCloudFlow(IOrganizationService service, Guid workflowid, Guid systemuserid)
{

    var workflow = new Entity("workflow", workflowid)
    {
        Attributes = {

        {"description", "This flow will ensure consistency across systems." },            
        {"statecode", new OptionSetValue(1) } //Turn on the flow.
     }
    };

    service.Update(workflow);
}

After a successful run, the flow will be turned on, and the description will change.

Description

Delete a cloud flow

The following examples show how to delete the workflow record that represents a cloud flow.

/// <summary>
/// Deletes a workflow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="workflowId">The id of the cloud flow to delete.</param>
public static void DeleteCloudFlow(IOrganizationService service, Guid workflowId) { 

service.Delete(entityName:"workflow",id: workflowId);

}

Main Method to call all the methods.

static void Main()
    {
        // ServiceClient implements IOrganizationService interface  
        IOrganizationService service = new ServiceClient(connectionString);
        
       var res = CreateCloudFlow(service); // Creates a cloud flow
        var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
        Console.WriteLine($"User ID is {response.UserId}.");
     
        UpdateCloudFlow(service, res, response.UserId); // Updates a flow


        DeleteCloudFlow(service, res); // Deletes a flow

        // Pause the console so it does not close.  
        Console.WriteLine("Press the <Enter> key to exit.");
        Console.ReadLine();
    }

Conclusion

Managing Power Automate flows via code allows teams to scale, automate, and govern their automation strategies. Whether using the Dataverse Web API or .NET SDK, you can integrate flow management into deployment pipelines, provisioning scripts, or administration portals.