Skip to content

LarsBauer/DataverseODataClient

Repository files navigation

Dataverse OData Client

Build status codecov NuGet Badge

This NuGet package provides a ready-to-use OData Client for Microsoft Dataverse Web API.

Features

  • based on the very popular and feature-rich Simple.OData.Client
  • seamless integration in dependency injection and configuration concepts of .NET
  • makes use of IHttpClientFactory to delegate HttpClient lifecycle to framework
  • token handling based on Azure Identity, which offers various ways to authenticate against Dataverse
  • supports e2e-traceability of requests via correlation id
  • developed and targeted at Azure Functions and Azure Web Apps

Get started

To get started with this package just install the latest version from NuGet using the dotnet CLI:

dotnet add package BauerApps.DataverseODataClient

After installation you can register the client in Startup.cs...

using System;
using DataverseODataClient.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace DataverseODataClient.Sample
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDataverseODataClient(options =>
            {
                options.OrganizationUrl = new Uri("https://my-organization.crm4.dynamics.com");
                options.ManagedIdentityClientId = "d0f19fa6-76ef-46cb-93ac-fcde5a4a6143"; // optional
            });
        }
    }
}

... and inject it for example into your Controller. As mentioned above this client basically is a preconfigured version of Simple.OData.Client, which means you can use it the same way. If you are new to Simple.OData.Client head over to the wiki for available features.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Simple.OData.Client;

namespace DataverseODataClient.Sample.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SampleController : ControllerBase
    {
        private readonly IODataClient _client;

        public SampleController(IODataClient client)
        {
            _client = client;
        }

        [HttpGet]
        public async Task<Account> Get(string id)
        {
            return await _client
                .For<Account>()
                .Key(id)
                .FindEntryAsync();
        }
    }
}

Configuration

You can configure Dataverse OData Client using DataverseODataClientOptions

Option Required Description
OrganizationUrl The base url of your Dataverse organization
ManagedIdentityClientId When using a Azure user-assigned managed identity for authentication you have to specify the client id of the corresponding managed identity.

Correlation Id

It is possible to pass a correlation id to Dataverse by registering a implementation of ICorrelationIdProvider. The id is then available as tag parameter in the shared variables. See Dataverse docs for details.

// sample correlation id provider
public class HttpHeaderCorrelationIdProvider : ICorrelationIdProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public HttpHeaderCorrelationIdProvider(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string GetCorrelationId()
    {
        // extract correlation id from http header
        return _httpContextAccessor.HttpContext?.Request.Headers["x-correlation-id"];
    }
}

// register in Program.cs
services.AddDataverseODataClient(options => { ... });

services.AddTransient<ICorrelationIdProvider, HttpHeaderCorrelationIdProvider>();