web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Dynamix Academy / How to Connect to Dynamics ...

How to Connect to Dynamics 365 using C# Console Application?

Abhishek Dhoriya Profile Picture Abhishek Dhoriya 1,013

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 connecting to dynamics CRM/ 365 from c#

Must have

  • Firstly, have the basic Functional understanding of Dynamics 365.
  • Secondly, have basic programming knowledge on .NET with C# or any other Object-Oriented Programming language.
  • Lastly, Visual Studio Community Edition 2015/2017 or Higher Installed.

Good to have

Step by Step: Connecting to Dynamics 365 using a C# Console App

  • Step 1: Firstly, Create a new Project of type C# Console Application in your Visual Studio. Give it meaningful Name and click on OK.
  • Step 2: Secondly, Now You need to add a NuGet Package called Microsoft.CrmSdk.CoreTools -Version 9.0.0.7. Or Click here to know the latest version of the CRM Dlls.
  • Step 3:Thirdly,  The current version Microsoft CRM SDK DLLs are installed in the project’s bin\coretools folder.
  • Step 4: Fourthly, Now you need to add the below dll references to the project.
    • Microsoft.Crm.Sdk.Proxy.dll
    • Microsoft.Xrm.Sdk.dll
    • System.Runtime.Serialization
    • System.ServiceModel
  • Step 5: Fifthly, Then add the below using statements or namespaces.
    • using Microsoft.Xrm.SDK;
    • using Microsoft.Xrm.SDK.Client;
    • using Microsoft.CRM.SDK.Messages;
    • using System.Net;
    • using System.ServiceModel.Description;
  • Step 6:  Then Project Setup is done. Now the main code is given below. Simply Copy and Paste the below code in the Console App Main method. Download the full working code from here.
IOrganizationService _service = null;
try
 {
 ClientCredentials clientCredentials = new ClientCredentials();
 clientCredentials.UserName.UserName = "<YourUserName>@<YourOrgName>.onmicrosoft.com";
 clientCredentials.UserName.Password = "<YourPassword>";

// Copy and Paste Organization Service Endpoint Address URL
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<YourOrgName>.api.<CRMRegion>.dynamics.com/XRMServices/2011/Organization.svc"),
 null, clientCredentials, null);
if (_service!= null)
 {
 Guid userid = ((WhoAmIResponse)_service.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: Now Code is ready, so Build the Solution. If the build is succeeded, Run the application to see the below Message.
c# in dynamics 365
C# in dynamics 365

Conclusion

In Conclusion, if you want to connect to dynamics 365 using c# then your first and foremost objective must be to get an Initialized object of IOrganizationService. And then you can use that object for all your CRUD operations in Dynamics 365 Console application.

So, I hope that you might have found this article and video useful and learned some useful tips from it. If so then, kindly share this article with your friends and colleagues. Also, share your feedback and If you still have any doubt or confusion then please put them in the comments below.

Or if you feel that, I have missed some points that you have faced then we highly encourage you to share with us and our audience. I love to hear back from you and you can also share your feedback about this article as well.

So on that note, I take your leave and thank you for reading this article till the end. Hope to see you all in the next article till then bye bye and take very good care of yourself and your friends and family.

Also, find More on Dynamix Academy :

Let’s Connect:

The post How to Connect to Dynamics 365 using C# Console Application? appeared first on Dynamix Academy.

Comments

*This post is locked for comments