Custom Connector as C# Sandbox for PowerAutomate Flow

Ever wanted to habe a Code Sandbox to run a script in Flow? I had this issue to calculate an SHA1 hash value which is not possible with builtin flow functions. Obvious solution would be to put it in an Azure Function. There is a solution by Valentin Gasenko to create a custom connector and utilize the Code feature.

You may create a custom connector that connects to nowhere😉 with a fake method and place C# code there. It has some limitations to an Azure Function e.g. you can’t install packages etc. but it might help for many other issues.

OpenAPI Definiton

Create a new OpenAPI Definition Swagger file that defines a method you would like to use in your flow. Here is an example for an API with a method called executeScript that takes a string as input and returns a string as output.

{
  "openapi": "3.0.0",
  "info": {
    "title": "Script Execution Service",
    "version": "1.0.0",
    "description": "A simple REST API that executes a script and returns text output."
  },
  "paths": {
    "/ExecuteScript": {
      "get": {
        "summary": "Executes a script and returns text output",
        "operationId": "executeScript",
        "parameters": [
          {
            "name": "input",
            "in": "query",
            "description": "The script input as a string",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful execution",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "output": {
                      "type": "string",
                      "description": "The output of the executed script"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid input provided"
          }
        }
      }
    }
  }
}

Create a custom connector in PowerAutomate

In PowerAutomate create a new custom connector. You may have to lookup the custom connector link by clicking on “… More” on the left pane.

Enable Custom Connector in PowerAutomate

Create a new custom connector by importing the OpenAPI definition file. Provide name and description if you like. Make sure to set the Host to valid URL. It doesn’t matter if there is no real host with this name. I use fakeapi.local in my example.

Create a custom connector that connects to nowhere

Place C# code

Go to the Code section and enable code by selection the slider. Choose the method you want to use. In my example it’s the executeScript Method. Copy the C# code you want to execute in the code editor window below. This is an example code that reads the input parameter from the request and returns it in a Hello statement.

public class Script : ScriptBase
{
  public override async Task ExecuteAsync()
  {
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    string url = this.Context.Request.RequestUri.PathAndQuery;
    string parsedUrl = url.Split('?')[1];

    var paramsCollection = HttpUtility.ParseQueryString(parsedUrl);
    string value = paramsCollection["input"];

    response.Content = CreateJsonContent("{\"message\": \"Hello "+value+"\"}");
    return response;
  }
}
Place C# script in the custom connectors code section

Save and Test

You have to select “Create Connector” before testing. If your code is not working you will get an error here. If the creation was successful you can test your connector.

Test C# sandbox in PowerAutomate

Use C# Sandbox in your Flow

Simply create a new flow or use an existing one. You we’ll find the connector under the custom connectors. In my example it has a single action to execute the code to which I pass the value of the variable Name.

Include C# sandbox in PowerAutomate Flow
C# code executed in PowerAutomate Flow

Like in the connector test scenario, it executes the C# script and returns Hello and the provided value.

Unknown's avatarAbout erpcoder
Azure Cloud Architect and Dynamics 365 enthusiast working in Research & Development for InsideAx

Leave a comment