Dataverse offers several connectors through Power Automate which makes it very easy to integrate data from all over the place, but there are specific scenarios that you might want to access Dataverse’s records from a custom implementation such as an Azure function, today we will see how we can achieve that.
Since your azure function is on Azure and not in the Dataverse we will need to send an authentication request to Dataverse to prove that our Azure function is allowed to surface the Dataverse and for that, we will need to create an application in Active Directory.
Register a new app and save the Application (client) ID that will be generated for you, we will need it later. Now we need to create a secret for our app, go to Certificates & secrets to do that.
Create a new secret and set a name and an expiration date.
IMPORTANT: Save the Value that will be generated as you won’t be able to see it again once you leave this page
Now the only thing left to do is to grant permissions to our newly created app to access the Dataverse.
Open the Power Platform admin center, find the environment that you are working on and go to Settings > Users and Application Users and select the application that was created earlier, assigning a security role to it.
You should now have an Azure APP with a Client ID associated to the new application user and with that appropriated security roles. The code below shows c C# implementation of the common method to generate the access token required to perform HTTP operations against the dataverse service URL.
string serviceUrl = "https://yourorg.crm4.dynamics.com/";
string clientId = "yourClientID";
string secret = "yourSecret";
AuthenticationContext authContext = new AuthenticationContext
ClientCredential credential = new ClientCredential(clientId, secret);
AuthenticationResult result = authContext.AcquireTokenAsync(serviceUrl, credential).Result;
string accessToken = result.AccessToken;