Connect to Dynamics 365 CRM Online using a client ID and secret in a C# console app


1. Prerequisites

  • Install the Dynamics 365 SDK assemblies. You can install the necessary NuGet packages, such as:
  • Microsoft.CrmSdk.CoreAssemblies
  • Microsoft.CrmSdk.XrmTooling.CoreAssembly
  • Register your app in Azure Active Directory (AAD) to retrieve the client ID, client secret, and tenant ID.

2. Code Implementation

The following code demonstrates how to authenticate and interact with Dynamics 365 CRM using the CRM SDK:

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;

class Program
{
    static void Main(string[] args)
    {
        string clientId = "Your_Client_ID";
        string clientSecret = "Your_Client_Secret";
        string tenantId = "Your_Tenant_ID";
        string crmUrl = "https://Your_CRM_Organization.crm.dynamics.com/";

        // Create connection string
        string connectionString = $@"
            AuthType=ClientSecret;
            ClientId={clientId};
            ClientSecret={clientSecret};
            TenantId={tenantId};
            Url={crmUrl};";

        // Establish connection
        CrmServiceClient serviceClient = new CrmServiceClient(connectionString);

        if (serviceClient.IsReady)
        {
            Console.WriteLine("Connected to CRM successfully!");

            // Example: Retrieve accounts
            IOrganizationService service = serviceClient.OrganizationServiceProxy;
            QueryExpression query = new QueryExpression("account")
            {
                ColumnSet = new ColumnSet("name", "accountnumber")
            };

            EntityCollection results = service.RetrieveMultiple(query);

            foreach (var entity in results.Entities)
            {
                Console.WriteLine($"Account Name: {entity.GetAttributeValue<string>("name")}, Account Number: {entity.GetAttributeValue<string>("accountnumber")}");
            }
        }
        else
        {
            Console.WriteLine($"Failed to connect: {serviceClient.LastCrmError}");
        }
    }
}

3. Explanation

  • Authentication: The connection string uses AAD authentication with the client ID, client secret, and tenant ID.
  • Connection: CrmServiceClient establishes a connection to Dynamics 365.
  • Query: The QueryExpression retrieves data from the CRM, such as accounts in this example.

Note : This article was created with assistance from AI and there could be mistakes / error

Canvas app doesn’t show data from related records.

Solution : Refresh the Datasource and then save and publish the app . Detailed description is given below .

Description
I created a power apps with 2 text boxes where i am fetching logged in user’s Business unit and then country where he belongs .
Now , to make it clear
– i am using Dynamics 365 as a data source .
– tables involved , systemuser (User), business unit , country
i added a data source for User’s table .
Approach :
i add a global variable on start on the app where i got the user object based on the logged in user’s email address .I stored it in a variable called D365

I set the value of other 2 text boxes which should have the logged in user’s Business unit ID and country as shown below
D365User.'Business Unit'.Country.Name
D365User.'Business Unit'.Country.Country

Issue : However when i run the app it shows the data correctly for the 2 textboxes . However when i publish the app it doesn’t show any data .It showed “Getting Data ….” then blank

Solution : click “Refresh” Data source in the data source menu as shown below

then save and publish the app .