Writing Dynamics C# Console App With Only Two Dependent Packages

Writing C# code for Dynamics 365 opens up numerous possibilities within the platform. It enables the ability to update data in Dynamics (Dataverse) and facilitates integration scenarios, such as connecting external systems with Dynamics. However, one of the common challenges we encounter is the dependency on the .NET version utilized by Dynamics, which currently mandates .NET 4.6.2 and entails several dependencies. These dependencies can present problems, particularly in larger projects. Surprisingly, even for a simple C# application, the list of dependencies can become quite extensive. To make matters worse, one of the dependencies, Microsoft.IdentityModel.Clients.ActiveDirectory, has been deprecated.

How do we get around so many dependent packages? What if we want to run our code under a newer .NET version like 6.0 or .net core 3.1? The good news is that we now have a great solution called Dataverse Client. The code is available on: GitHub PowerPlatform DataverseServiceClient

What’s really nice is that there are only two dependencies to run a simple console application:

DataverseCRUD.cs

Here is a sample CRUD program that connects to Dynamics 365, then create an account, read the account, update it and then delete it.

using System;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Extensions.Configuration;

namespace DataverseClientCRUD
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                     .AddJsonFile("appsettings.json", true, true);
            IConfiguration config = builder.Build();
            string connectionString = config["ConnectionStrings:MyCRMServer"];
            using var serviceClient = new ServiceClient(connectionString);

            CRUD(serviceClient);
        }

        private static void CRUD(ServiceClient serviceClient)
        {
            //Create
            Console.WriteLine("**** Create ****");
            Entity account = new Entity("account");
            account["name"] = "ABC Corporation";
            account.Id = serviceClient.Create(account);

            //Read
            Console.WriteLine("**** Read ****");
            Entity readAccount = serviceClient.Retrieve(
                entityName: account.LogicalName,
                id: account.Id,
                columnSet: new ColumnSet("name")
            );
            Console.WriteLine("Retrieved account name: {0}", readAccount["name"]);

            //Update
            Console.WriteLine("**** Update ****");
            account["name"] = "ABC Corp";
            serviceClient.Update(account);

            //Delete
            Console.WriteLine("**** Delete ****");
            serviceClient.Delete(account.LogicalName, account.Id);
            serviceClient.Dispose();
        }
    }
}

appsettings.json

Create an appsettings.json file in Visual Studio and set the file to copy to output directory:

 

{
  "ConnectionStrings": {
    "MyCRMServer": "AuthType=ClientSecret;Url=https://XXXX.crm.dynamics.com;ClientId=XXXX;ClientSecret=XXXX"
  }
}

Dynamics 365 Interactive Dashboard Showing Inaccurate Data

Interactive Dashboards in Dynamics 365 are highly valuable and offer crucial business insights into the data. However, I encountered an issue recently where the chart counts did not align with the stream counts. While the dashboard displayed the accurate data on the development server, the production server failed to reflect the correct chart counts in comparison to the stream (Grid on the left side). It’s important to note that the stream did display the correct counts, but the individual chart components did not.

Creating a new interactive dashboard fixed the problem, however I had end-users creating customized filters and I didn’t want to have to train them on how to recreate the filters. 

I decided to create a new solution with just the dashboard component and compare the solutions between the development copy and production copy. My handy Notepad++ compare utility showed me what’s different. Somehow during the deployment to production, the default view was pointing to a different GUID. I repackaged the solution and deployed to production and it started working again. Hopefully this will help others.

Dynamics 365 WebAPI Asynchronous Calls

Dynamics 365 development is a constantly evolving field, with the JavaScript API being no exception. Exciting advancements have been made in this space, such as the introduction of WebApi’s method, which allows for basic CRUD operations (Link).

Gone are the days of worrying about including JSON files or calling jQuery libraries just to make simple API calls. The platform now provides straightforward methods for performing basic CRUD operations.

To illustrate this, here’s a simple example. I utilized the fantastic REST Builder tool to generate the code. This code searches for an email address and retrieves the full name of the corresponding contact record.

function FindContactRecord(email) {
    Xrm.WebApi.online.retrieveMultipleRecords("contact", "?$select=fullname&$filter=(emailaddress1 eq '" + email + "')&$top=1").then(
        function success(results) {
            var result = results.entities[0];
            var fullname = result["fullname"];
            ProcessData(fullname);
        },
        function (error) {
            console.log(error.message);
        }
    );
}

The above code works great for a single record, since the ProcessData function gets invoked in the success function of the WebAPI call. When we start to do bulk operations, the code starts executing asynchronously. Here is an example of calling the function multiple times. This code is only for illustrations purposes. If we were to query multiple contact records, our best practice is to batch multiple email addresses into a single WebAPI call.

function ProcessData(EmailArray) {
    var contactFullnames = new Array();
    for (var i = 0; i < EmailArray.length; i++) {
        contactFullnames.push(FindContactRecord(email[i].trim()));
    }
    return contactFullnames;
}

function FindContactRecord(email) {
    Xrm.WebApi.online.retrieveMultipleRecords("contact", "?$select=fullname&$filter=(emailaddress1 eq '" + email + "')&$top=1").then(
        function success(results) {
            var result = results.entities[0];
            var fullname = result["fullname"];
            return (fullname);
        },
        function (error) {
            console.log(error.message);
        }
    );
}

//Main function 
var contactFullnames = FindContactRecords(EmailArray);
ProcessData(contactFullnames);

The main issue we face with the above code is that the contactFullname array will not return any elements since JavaScript engine is executing the WebAPI calls asynchronously for performance reasons.

So how do we fix this problem? By using async, await and then statements, JavaScript will return a promise which will not execute subsequent code until the async code completes processing. The changes in blue illustrates the required changes.

async function ProcessData(EmailArray){
    var contactFullnames = new Array();
    for (var i = 0; i < EmailArray.length; i++) {
       await contactFullnames.push(FindContactRecord(email[i].trim()));
    }
    return contactFullnames;
}

async function FindContactRecord(email) {
    await Xrm.WebApi.online.retrieveMultipleRecords("contact", "?$select=fullname&$filter=(emailaddress1 eq '" + email + "')&$top=1").then(
        function success(results) {
            var result = results.entities[0];
            var fullname = result["fullname"];
            return (fullname);
        },
        function (error) {
            console.log(error.message);
        }
    );
}

//Main function 
FindContactRecords(EmailArray).then(
    contactFullnames => ProcessData(contactFullnames)
);

There are many articles that discusses async/await, but how to applies to Dynamics 365 is not something well covered. I hope this  article helps.

Ongo Homes of your back office systems provides 10,000 homes for people

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Ongo Homes of your back office systems provides 10,000 homes for people

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Virtual environment in order to scale up their back office of your back office systems

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Ongo Homes of your back office systems provides 10,000 homes for people

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Virtual environment in order to scale up their back office of your back office systems

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Ongo Homes of your back office systems provides 10,000 homes for people

Overview

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Tenet has remained constant: we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Problems

Sure, the technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Challenge

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.

Solution

Technology has come a long way since then, and the variety of the information objects we’re managing has changed a lot, but one tenet has remained constant we’ve always focused on the intersection of people, processes, and information. As the Association for Intelligent Information Management, we help organizations put their information to work.