Connect to Dynamics CRM from Azure Function using new Dataverse service client

Rehan KhanDyn365CE2 years ago14 Views

In this blog We will implement Dataverse service client which was earlier in public preview and now available generally. Connecting to Dataverse is now easier with just few lines of code .
For .NET Core applications there is finally an official SDK available providing the same feature set known from Xrm Tooling. This package is intended to work with .NET full framework 4.6.2, 4.7.2 and 4.8, .NET core 3.0, 3.1 and 5.0. Library’s source code is available at Github.

In todays blog we will connect to Dataverse from Azure function which is running on .Net core 3.1.
Below are the pre requisite

  • Register a new App in Azure for dynamics 365. You can refer this blog .
  • Create a new Azure Function, and ensure it compiles correctly.
  • Use the following code snippet to connect to the Dataverse.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;

namespace Dataverse_connection
{
    public static class DataverseConnection
    {
        [FunctionName("DataverseConnection")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var _contactid = new Guid();

            try
            {
 string _clientId = "b9d8b646-fdd5-44cd-*****-***********";
 string _clientSecret = "khv8Q~B4qvMudtKMKYk0gOQ*****************";    
 string _environment = "reydynamic.crm8";
  var _connectionString = @$"Url=https://{_environment}.dynamics.com;AuthType=ClientSecret;ClientId={_clientId}
                ;ClientSecret={_clientSecret};RequireNewInstance=true";


                var service = new ServiceClient(_connectionString);
                if (service.IsReady)
                {
                    // Create a contact 
                    Entity contact = new Entity("contact")
                    {
                        ["firstname"] = "Rey",
                        ["lastname"] = "Dynamics CRM"
                    };
                    _contactid = service.Create(contact);
                }


            }
            catch (Exception ex)
            {
                return new OkObjectResult(ex.Message);
                throw new(ex.Message);
            }
            return new OkObjectResult("Contact Record created with ID " + Convert.ToString(_contactid));
        }
    }
}

In the above azure function I am simply creating a contact record using Service client.
Next step is to publish the azure function and test it in postman or browser.

Testing the Azure function on Postman .

In the above screenshot you can see contact record is created successfully.

A record is created successfully in Dataverse CRM.
This is how we can connect to Dataverse with few lines of code.
Thanks for reading …. Hope this helps !
You can follow me on my Linked in Rehan khan!

Original Post https://msdynamicscrm137374033.wordpress.com/2022/08/18/connect-to-dynamics-crm-from-azure-function-using-new-dataverse-service-client/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...