Showing posts with label ConsoleApp. Show all posts
Showing posts with label ConsoleApp. Show all posts

Wednesday, June 29, 2022

Console application to connect to Dynamics 365 On-premise using AuthType IFD

Console Application code to connect to Dynamics 365 on-premise,  follow the below step-by-step process to connect, using AuthType as IFD

1. Open Visual Studio -> File -> New -> Project -> Search & Select C# Console Application (.Net Framework) -> Click Next 














2. Give a suitable name for your project -> Click Create

3. Add all the references 
Click on Refernces -> Manage NuGet Packages -> Search for Microsoft.Xrm.Tooling.CoreAssembly  and install it


4. Add below references to your program.cs file

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Tooling.Connector;

5.  Below is the code to connect to Dynamics 365 On-Premise pass Domain, AuthType = IFD, user Name, Password, and URL

 IOrganizationService oServiceProxy;

            //Create the Dynamics 365 Connection:
            CrmServiceClient oMSCRMConn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("Domain=<DomainName>; AuthType=IFD;Username=<User Email ID>;Password=<EnterPassword>;URL=https://<URL>.com/OrgName;");

            //Create the IOrganizationService:
            oServiceProxy = (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient != null ? (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient : (IOrganizationService)oMSCRMConn.OrganizationServiceProxy;
            if (oServiceProxy != null)
            {
                //Get the current user ID:
                Guid userid = ((WhoAmIResponse)oServiceProxy.Execute(new WhoAmIRequest())).UserId;

                if (userid != Guid.Empty)
                {
                    Console.WriteLine("Connection Successful!");
                }
            }
            else
            {
                Console.WriteLine("Connection failed...");
            }

6. Below is the complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Tooling.Connector;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IOrganizationService oServiceProxy;

            //Create the Dynamics 365 Connection:
            CrmServiceClient oMSCRMConn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("Domain=XXXX; AuthType=IFD;Username=XXXXX;Password=XXXXX;URL=XXXXXXX;");

            //Create the IOrganizationService:
            oServiceProxy = (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient != null ? (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient : (IOrganizationService)oMSCRMConn.OrganizationServiceProxy;
            if (oServiceProxy != null)
            {
                //Get the current user ID:
                Guid userid = ((WhoAmIResponse)oServiceProxy.Execute(new WhoAmIRequest())).UserId;

                if (userid != Guid.Empty)
                {
                    Console.WriteLine("Connection Successful!");
                }
            }
            else
            {
                Console.WriteLine("Connection failed...");
            }

        }

    }

}

7.  This is how the code looks in the Visual Studio:


8. Test -> Build & Debug 




That's all.....

Thank you!!!
Happy CRMing...!!!