Connect to Dynamics 365 CRM in C# Console App
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.
Comments
-
Connect to Dynamics 365 CRM in C# Console AppThanks a lot for the detailed blog.When I try to use the same I am getting an errorAADSTS50079: Due to a configuration change made by your administrator, or because you moved to a new location, you must enroll in multi-factor authentication to access '00000007-0000-0000-c000-000000000000'.
Trace ID: e0b58068-b21b-4977-9cef-f078e6d31f00
Correlation ID: c012f1c1-98b3-40e3-a990-f88ef48a228e
Timestamp: 2023-09-25 04:09:03Z => Response status code does not indicate success: 400 (BadRequest). => {"error":"interaction_required","error_description":"AADSTS50079: Due to a configuration change made by your administrator, or because you moved to a new location, you must enroll in multi-factor authentication to access '00000007-0000-0000-c000-000000000000'.\r\nTrace ID: e0b58068-b21b-4977-9cef-f078e6d31f00\r\nCorrelation ID: c012f1c1-98b3-40e3-a990-f88ef48a228e\r\nTimestamp: 2023-09-25 04:09:03Z","error_codes":[50079],"timestamp":"2023-09-25 04:09:03Z","trace_id":"e0b58068-b21b-4977-9cef-f078e6d31f00","correlation_id":"c012f1c1-98b3-40e3-a990-f88ef48a228e","error_uri":"https://login.microsoftonline.com/error?code=50079","suberror":"basic_action"}: Unknown errorUnable to Login to Dynamics CRM
Unable to Login to Dynamics CRMOrganizationWebProxyClient is null
OrganizationWebProxyClient is null
Though removed all the MFA, still getting this error. Appreciate any help on this.
*This post is locked for comments