Using C# code in custom connector to connect to Dataverse?

By | September 24, 2021

This one is almost of an academical interest; since, after all, there is Dataverse connector available already. But,  just on the principle, I was wondering if it were possible to use C# code in the custom connector to connect to Dataverse?

Actually, there is nothing special about Dataverse – the same approach could be used with other cloud services. Basically, all we need is:

  • Figure out how to do Azure authentication
  • Write some code to call required API-s
  • Process results and return them to the calling Power Automate flow

And, of course, may need to keep in mind 5 seconds limit, which is making it even more academical in a way (since some calls are just meant to take longer, and there is nothing we can do about it).

Anyways, turned out the above is totally doable and not even that complicated.

First things first – we will need to register an app in Azure (unless you have one already) to configure Azure Authentication for the connector.

The process is described here: https://docs.microsoft.com/en-us/powerapps/developer/data-platform/walkthrough-register-app-azure-active-directory

Below are a few screenshots from my azure portal

There is am application:

image

There is a secret:

image

You may also need to grant administrative consent for the API-s:

image

And there is a redirect URI:

image

As for the redirect URI, you are, actually, supposed to add it to the application once you have created the connector (see below). Although, I have a feeling it’s going to be the same URI in this case.

Still, having registered the application, you can start setting up the connector:

image

Just like we did it before, you can use pretty much anything for the host name (this time I figured I’d go with microsoft.com):

image

On the security tab, start with the swagger file – you can get it from git:

https://github.com/ashlega/ITAintBoringITAFunctions/tree/main/ITACDSConnectorFiles

image

Close swagger editor and configure remaining part of the security tab as per the screenshot below:

image

Use OAuth 2.0 for the authentication type.

Use Azure Active Directory for the identity provider.

For the client ID, use application ID from the Azure Portal.

For the client secret, use client secret from the Azure Portal for the application you registered above

For the resource URL, use your environment url.

Then proceed to the “definition” stage, and to the “Code” right away – enable code, and copy paste contents of Script.cs file from git: https://github.com/ashlega/ITAintBoringITAFunctions/tree/main/ITACDSConnectorFiles

image

Make sure to replace the url there with your own environment url.

The interesting part about that code is that, essentially, we can just reuse incoming Request. There is authentication token there, already, so we don’t need to worry about that part at all.

Now you can create the connector, and you should be able to test it from there. Once you try testing it,it will ask you to create a connection first, which is where you’ll provide your credentials:

image

Once the connection is there, you can run the actual test – enter part of the user name, and the connector will return a json array with all the users whose name matches:image
Then, of course, you can use it in your Flows.

Is there practical value there? Not sure to be honestSmile But, I guess, if you wanted to quickly create a simplified re-usable Dataverse connector which would do some targetted/specific things in Dataverse, you could do it this way without having to go through the process of setting up out-of-the-box Dataverse connector actions. And this applies to other Azure services, too (we could probably do the same with Sharepoint etc).

Interested to know more? Here are a couple of other posts:

C# code in Power Automate? No way…

C# code in Power Automate: let’s sort a string array?

Leave a Reply

Your email address will not be published.