Thursday, March 23, 2023

Easy way to Connect to D365 Using console app

Step 1: Open Visual Studio -> Create a New Project with Console App (.NET Framework)


Step 2: Fill in all the details as shown and Click Create

Step 3: Open references and add the below library using the NuGet package


Step 4: Open the class file and add the below code and click on save

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

namespace ConsoleAppProjects
{
    public class D365ConnectionService
    {
        public static IOrganizationService service;
        public static IOrganizationService ConnectionService()
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = https://orgd***.crm8.dynamics.com/;Username=Amresh@CRM***.onmicrosoft.com;Password=D**@1234";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                service = conn.OrganizationWebProxyClient ?? (IOrganizationService)conn.OrganizationServiceProxy;
                if (service != null)
                {
                    Console.WriteLine("Connected Successfully....");
                    //Executing YOUR CRM OPERATIONS.
                }
                else
                {
                    Console.WriteLine("Connection failed....");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
            return service;
        }
    }
}

Step 5: Start and test.





That's all...
Thank you!!!

No comments:

Post a Comment