How to Connect to Dynamics 365 using C# Console Application?
Abhishek Dhoriya
1,011
Article on How to connect to dynamics 365 from c#? In this article, we have explained how to connect to Dynamics CRM 365 online using C# example with a demo code as well?
Step by Step to connect Dynamics 365 CRM Online V9.X using C# Console Application
Pre-Requisites before you start writing code for connect to dynamics 365 from c#
Must have
- Basic Functional understanding of Dynamics 365.
- Basic programming knowledge on .NET with C# or any other Object-Oriented Programming language.
- Visual Studio Community Edition 2015/2017 or Higher Installed.
Good to have
- Dynamics 365 SDK Installed Video Here. SDK Download URL
- Get Dynamics 365 trial instance here
- Step by Step Setup Dynamics 365 trial instance setup URL
Step by Step: Connecting to Dynamics 365 using a C# Console App
- Step 1: Open Visual Studio and Click on New Project. Click on Visual C# and select Console Application.
- Give Name to the Project and click on OK.
- Step 2: Go to Visual Studio Tools -> NuGet Package Manager and click on Package Manager Console.
- Step 3: In Package Manager Console, Copy & Paste the below command to get the required CRM SDK Dlls and press Enter.
- Click here to know the latest version of the CRM Dlls.
- Install-Package Microsoft.CrmSdk.CoreTools -Version 9.0.0.7
- Step 4: Latest CRM SDK DLLs are installed in the project’s bin\coretools folder. You can also see the success message in the Package Manager Console.
- Step 5: Right Click on References and navigate to the bin\coretools folder. Add the below Dlls to the Visual Studio project.
- Microsoft.Crm.Sdk.Proxy.dll
- Microsoft.Xrm.Sdk.dll
- System.Runtime.Serialization
- System.ServiceModel
- Step 6: Add the below namespaces.
- using Microsoft.Xrm.SDK;
- using Microsoft.Xrm.SDK.Client;
- using Microsoft.CRM.SDK.Messages;
- using System.Net;
- using System.ServiceModel.Description;
- Step 7: Copy and Paste the below code in the Main method.
IOrganizationService organizationService = null;
try
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>.onmicrosoft.com";
clientCredentials.UserName.Password = "<ProvideYourPassword>";
// For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
// Copy and Paste Organization Service Endpoint Address URL
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<ProvideYourOrgName>.api.<CRMRegion>.dynamics.com/XRMServices/2011/Organization.svc"),
null, clientCredentials, null);
if (organizationService != null)
{
Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
Console.WriteLine("Connection Successful!...");
}
}
else
{
Console.WriteLine("Failed to Established Connection!!!");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught - " + ex.Message);
}
Console.ReadKey();
- Step 8: Right-click on the solution and build it. If the build is succeeded, Run the application to see the below Message.
Also, find More on Dynamix Academy :
- Unified Interface in Microsoft Dynamics 365
- Microsoft Dynamics 365 V9.0 New Features
- How to Debug Asynchronous Workflow in Dynamics 365
- Impersonation In Microsoft Dynamics 365/CRM Plugins
- Microsoft Dynamics CRM/365 Interview Question and Answers
You can also watch our Videos on playlist:
- History of Microsoft Dynamics 365: https://bit.ly/2w0uwjL
- Dynamics 365 – Custom Workflow Development Course: https://bit.ly/2w1Ixhh
- Microsoft Dynamics CRM: https://bit.ly/2WPhcdF
- Microsoft Dynamics 365 Business Central Introductory Course: https://bit.ly/2Q8s2cm
- Plugin Development in Microsoft Dynamics 365: https://bit.ly/2VwIDYm
Let’s Connect:
- Instagram: https://bit.ly/2YBdchI
- WhatsApp: https://bit.ly/2JIN7Jf
- Twitter: https://goo.gl/d84Qw9
- Facebook: https://goo.gl/UQH3Eg
- LinkedIn; https://bit.ly/2VwtOoO
The post How to Connect to Dynamics 365 using C# Console Application? appeared first on Dynamix Academy.
*This post is locked for comments