C# code in Power Automate? No way…

By | September 14, 2021

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:

image

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:

image

2. To make it simple, let’s use no security

image

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:

image

5. And that’s about it – you can test it now

image

From there, just create a flow, pick your new connector, and use it in the flow:

image

And do the test:

image

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/

4 thoughts on “C# code in Power Automate? No way…

  1. Philippe

    Many thanks for this very interesting information. I’m using it as inspiration to create a connector to decode a hexadecimal encoded URL. I use the standard C# function “UrlDecode”. I’ve taken your code as is and modified it as follows:

    response.Content = new StringContent(HttpUtility.UrlDecode(contentAsString, “ISO-8859-1”))

    Unfortunately this does not work. Do you have an idea?
    Thanks for your help.

    Reply
  2. Philippe

    Many thanks for this very interesting information. I’m using it as inspiration to create a connector to decode a hexadecimal encoded URL. I use the standard C# function “UrlDecode”. I’ve taken your code as is and modified it as follows:

    response.Content = new StringContent(HttpUtility.UrlDecode(contentAsString, “ISO-8859-1”))

    Unfortunately this does not work. Do you have an idea?
    Thanks for your help.

    Translated with http://www.DeepL.com/Translator (free version)

    Reply
  3. sharp dressedman

    Any idea why the custom connector goes to 404 in a day? Basically this is all nice and dandy but can’t be used since all custom connectors made like this disappear after 24 hours.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *