Follow the below steps,
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: Goto Visual Studio Tools -> NuGet Package Manager and click on Package Manger 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 Package Manager Console.
Step 5: Right Click on References and navigate to bin\coretools folder.
Add the below Dlls to the Visual Studio project.
Microsoft.Crm.Sdk.Proxy.dll
Microsoft.Xrm.Sdk.dll
And also add the below and click on OK,
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 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 Established Successfully..."); } } 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 build is succeeded, Run the application to see the below Message.
Note:
Problem: If you face any build issues like the below, while building the project, then use the below solution and resolve it.
Solution: Follow the below steps to resolve the problem.
- Change the Project’s Dot Net Framework from 4.5 to 4.5.2, this change is required because latest CRM SDK Dlls needs the 4.5.2 version of Dot Net Framework.
2. Click on Yes and build the project.
For more info Click here.
Hope you have successfully connected to Dynamics CRM Online Version 9.X :):):)
This worked great. Thank you.
LikeLike
Welcome 🙂
LikeLike
Awesome Tool
LikeLike
Nice post, it works like a charm!
LikeLike
Hi I am facing the issue “Exception caught – An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.”..Can please suggeest for it
LikeLike
Thanks
LikeLike
Thanks a lot. Was searching for this for long
LikeLike
super bhayya 🙂
LikeLike
Great man. you are great!.
LikeLike
Thank you 🙂
LikeLike
Thank you, excellent introduction to XRM C# Console App.
LikeLike
Thank you so much really helpful 🙂
LikeLike
thanks
LikeLike
Thank you!
LikeLike
Great article! I spent 3 hrs trying to connect to CRM with MS docs and without success.
LikeLike
Awesome article. I just started learning C# and had no idea how to use Visual Studios. I spent close to a week trying to understand the VS, packages, References, namespaces, etc. and then a colleague sent this article over. Was able to connect in 30 minutes and understand exactly what you’re explaining.
Thank you!
LikeLike
awesome demonstation
LikeLike
Hi Arun,
Thanks for this code and it worked really well.i didnot used the latest SDK Dll and i have used v8.0.2.1
and it worked for me very well.
LikeLike
Hi Arun,
This is really works Great
Thank you…
LikeLike
Great !
Last version of CRM dlls : 9.1.0.25. It works with .Net Framework 4.6.2 or above.
Thanks.
LikeLike
Awsome blog Bro!
Even I have followed the same Step here Video on How to Connect to Dynamics 365 Using C#? http://www.dynamixacademy.com/connect-to-dynamics-365-using-c-console-application/
LikeLiked by 1 person
Great blog I have use some information’s for my contact manager app . This really help in me in my learning curve. Thanks
LikeLike