Power Automate flow is consistently evolving. At times there are UI changes or at times there are under the hood tweaks, but one thing is for sure Power Automate flow has matured a lot since its inception.
Today, in this blogpost, I will be discussing about the new feature that has been introduced recently in Power Automate.
Currently, we have plethora of connectors to choose from when it comes to getting our work done. However, there is also a way to create Custom Connector to bridge the gap with missing functionalities. The enhancement in question i.e., the usage of C# code has been made a part of the Custom Connector. Let me walk you through the process of creating a custom connector and the way to add the C# code. I will add the support of Math.Round, Math.Ceiling and Math.Floor using C#. With the introduction of C# support, we can do a lot many things.
Steps to create Custom Connector with C# code:
- I will be adding this in the solution, so I will begin by creating a custom connector in the solution. The other way of creating a custom connector is by going to Data -> Custom Connector.
- At the time of writing this blog, this feature is in Preview, meaning it is not recommended to be used in production.
Step 1:
In the solution where you want to add the Custom Connector, click on New -> Scroll to the bottom and click Other -> Select Custom Connector.
Step 2:
Give a name to your Connector Math, provide a Description and a Host.
Host does not matter in the case of Custom Connector with C# code
Step 3:
Let the Authentication Type be No Authentication.
Step 4:
On the Definition tab, Click on New action on the left-hand side. Create one with the below details.
For Ceiling and Floor Actions (while adding it later in the stage) replace Operation ID with MATH_CEILING and MATH_FLOOR respectively. Also, modify Summary and Description accordingly.
Store MATH_ROUND, MATH_CEILING and MATH_FLOOR for future use.
Step 5:
Add Request for the action, click on Import from sample.
On the Import from sample screen, add the below details and click Import
For feasibility, below is the JSON you can copy paste.
{
"Number": 1.5
}
For the Ceiling & Floor Actions (while adding it later in the stage), replace URL with /math-ceiling and /math-floor respectively. Other details remain same.
Step 6:
Time to tweak the body a bit. Click on the 3 dots next to body.

Now, click on 3 dots next to Number.
Fill in the details as below,
Step 7:
Time to define the Response. Click on Add default response.
Add the values as below and click Import.
{
"value": 1.5
}
Step 8:
Edit Response by clicking on the three dots on value.
Add the information as below,
Step 9:
Click Create Connector and from time to time keep clicking on Update Connector for the changes to be saved.
Step 10:
Repeat Steps from 4 to 9 for Ceiling and Floor operations.
Step 11:
Enable the Code and Select all the 3 Actions created before.
Step 12:
Copy and paste the below code in the editor.
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
return await this.HandleMathOperation().ConfigureAwait(false);
}
private async Task<HttpResponseMessage> HandleMathOperation()
{
HttpResponseMessage response;
// Get the Operation Id
string operationId = (string)this.Context.OperationId;
var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
// Parse as JSON object
var contentAsJson = JObject.Parse(contentAsString);
// Get the Number
double number = (double)contentAsJson["Number"];
// Based on the Operation Id perform the right action
if(operationId == "MATH_ROUND")
number = Math.Round(number);
else if(operationId == "MATH_CEILING")
number = Math.Ceiling(number);
else if(operationId == "MATH_FLOOR")
number = Math.Floor(number);
JObject output = new JObject
{
["value"] = (float)number,
};
response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = CreateJsonContent(output.ToString());
return response;
}
}
Step 13:
Update the connector and on the next step test the newly created connector.
Few things to note:
- Code takes precedence over codeless definition
- Only one C# script can be added per Custom Connector
- Code must be in C#
- Max execution time cannot execute 5 seconds
- File size cannot be more than 1 MB
- C# code needs to be tested locally
I feel even with the limitations stated above the opportunities that it opens is infinite.