Access Dataverse Web API from .NET Core with Microsoft.PowerPlatform.Dataverse.Client

For applications targeting the full .NET framework the process of connecting to the Dataverse Web API is usually handled by the XrmTooling SDK and the use of connection strings.

For .NET Core applications their is finally (in public preview) 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 the upcoming sample we will build an application with authentication via client credentials to access the Dataverse Web API.

Application registration in Azure Directory

We assume that you did create an app registration upfront and configured Dynamics impersonation permissions with clientId and client secret. The process is fully documented in docs.microsoft.com.

Create .NET Core Console Application (with Visual Studio Code)

Next step is to create a .NET Core Console App with the required dependency on Microsoft.PowerPlatform.Dataverse.Client.

mkdir "connect-to-dataverse-webapi-with-dataverse-client" cd "connect-to-dataverse-webapi-with-dataverse-client" dotnet new console dotnet add package Microsoft.PowerPlatform.Dataverse.Client code .

Replace the code in Program.cs with the code below.

Things to note:

  • Connection is configured with established pattern of connection strings.
  • The library has async/await support.

Edit lines 13-15 by setting clientId and clientSecret.

In line 21 the Dataverse Web API is queried for 10 accounts along with their name.

using Microsoft.PowerPlatform.Dataverse.Client; using Microsoft.Xrm.Sdk.Query; using System; using System.Linq; using System.Threading.Tasks; namespace connect_to_dataverse_webapi_with_dataverse_client { class Program { static async Task Main(string[] args) { const string clientId = "<clientId>", clientSecret = "<clientSecret>", environment = "<yourEnvironment>.crm4"; var connectionString = @$"Url=https://{environment}.dynamics.com;AuthType=ClientSecret;ClientId={clientId};ClientSecret={clientSecret};RequireNewInstance=true"; using var serviceClient = new ServiceClient(connectionString); var accountsCollection = await serviceClient.RetrieveMultipleAsync(new QueryExpression("account") { ColumnSet = new ColumnSet("name"), TopCount = 10 }); Console.WriteLine(string.Join("\n", accountsCollection.Entities .Select(x => $"{x.GetAttributeValue<string>("name")}, {x.Id}"))); } } }

Room for improvement

  • Move the auth constants to app.config (console) or local.settings.json (Azure Function) file and load the values as environment variables
  • Better error handling

Blog post written by

Marius Pothmann

Marius Pothmann is a Microsoft certified solution architect focusing on the Power Platform and cloud-native application development on Azure. Working on the Microsoft stack for over a decade gave him the opportunity to deep dive into many building blocks of successful enterprise application architectures like Dynamics 365, SQL Server, ADFS, ASP.NET (Core), SPA-based Web Apps, WPF/WinForms. If you think he can assist in solving your challenges drop us an e-mail and we will get back to you right away.