Introduction:
Connect to Dynamics 365 CRM in a C# Console App is a common requirement for developers who build applications that need to access and manipulate data stored in Dynamics CRM.
In this blog, we will explore how to connect to Dynamics 365 CRM in a C# Console App and retrieve data from it.
Test Environment:
- Dynamics 365 CRM online
- Dynamics 365 CRM Username and Password
- Visual Studio 2022
- .net framework 4.6.2 developer pack
Tips
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
Step 4: Create a connection string Create a connection string to connect to Dynamics 365 CRM.
Replace the "orgname" and "username" placeholders with your Dynamics 365 CRM organization name and username, respectively.
string connectionString = $"AuthType=Office365;Url=https://orgname.crm.dynamics.com/;Username=username@orgname.onmicrosoft.com;Password=password";
Step 5: Connect to Dynamics 365 CRM Create a new instance of the CrmServiceClient class and pass in the connection string as a parameter.
This will establish a connection to Dynamics 365 CRM.
CrmServiceClient service = new CrmServiceClient(connectionString);
Step 6: Execute WhoAmIRequest Request to test the connection
WhoAmIResponse whoAmIResponse = (WhoAmIResponse)service.Execute(new WhoAmIRequest()); var currentUserid = whoAmIResponse.UserId.ToString();
Step 7: Press F5 to test it.
Conclusion
That's it! You have successfully connected to Dynamics 365 CRM in a C# Console App. With this connection, you can perform various operations on Dynamics 365 CRM, such as creating, updating, and deleting records.

Like
Report
*This post is locked for comments