C# code in Power Automate? No way…
Did you know that you can easily add C# code to your Power Automate flows?
And you will need no azure functions, no plugins, no external web services. You will only need 5 minutes.
Although, your code will only have 5 seconds to run, and you will be limited in what exactly you can do in that code, but still:
The code above will simply convert given string value to uppercase – that’s not much, but you should get the idea.
Here is how you do it:
1. In the maker portal, go to Data->Custom Connectors, and start creating a new connector
You will need to provide the host name… Want to try google.com? That’s fine. One there is code, it’s going take over the codeless definition, so here we go:
2. To make it simple, let’s use no security
3. Now, for the definition, you might want to use the swagger below
swagger: '2.0' info: {title: TestCodeConnector, description: Test Code Connector, version: '1.0'} host: google.com basePath: / schemes: [https] consumes: [] produces: [] paths: /: post: responses: default: description: default schema: {type: string} summary: StringToUpperCase operationId: stringtouppercase parameters: - name: value in: body required: false schema: {type: string} definitions: {} parameters: {} responses: {} securityDefinitions: {} security: [] tags: []
This just says that a string comes in, and a string comes out.
4. Finally, for the code, you can use something the example below (my apologies for the formatting – it seems there are some special characters, so copy-paste would not work otherwise)
public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
return await this.HandleToUpperCase().ConfigureAwait(false);
}
private async Task<HttpResponseMessage> HandleToUpperCase()
{
HttpResponseMessage response;
var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(contentAsString?.ToUpper());
return response;
}
}
That one above will take a string and upper case it:
5. And that’s about it – you can test it now
From there, just create a flow, pick your new connector, and use it in the flow:
And do the test:
There are a few more notes:
- This is a preview feature
- You can find some extra details in the docs: https://docs.microsoft.com/en-us/connectors/custom-connectors/write-code
- Custom code execution time is limited by 5 seconds
- Only a limited number of namespaces is available for you to use
- When there is code, it takes precedence over the codeless definition. In other words, no API calls are, actually, made. Unless you make them from the code
So, there are limits. But, then, there are opportunities. There is a lot we can do in the code in 5 seconds.
PS. And, by the way, don’t forget about the upcoming PowerPlatform Chat event: https://www.itaintboring.com/itaintboring-powerplatform-chat/
This was originally posted here.
*This post is locked for comments