Dynamics 365 Online connection with C# Console Application
Microsoft have been adding new stuff and deprecating some to Dynamics 365 lately. And recently, I needed to create an app using C# console application to connect to Dynmaics365 online trial environment. I used the same code from another console application that I had implemented in 2019 and the connection was continuously getting rejected. After spending some time online for the root cause, I found out, to connect to the online instance of Dynamics365, one must now provide Azure appid with “AuthType=OAuth” where as earlier it was “AuthType=Office365” along with other necessary information in the connection string. In case you don’t have appid and redirecturi, universal ones are available.
Connection string looks like this: “AuthType=OAuth;Username=<username>;Password=<password>;Url=<dynamics365domainurl>;appid=<guid>;redirecturi=<callbackurl>”
Below I have added a quick step-by-step guide to create a console application in C# and connect it to Dynamics365 online instance using CrmServiceClient with connection string.
- Create a new project in Visual Studio (I have used Visual Studio 2019). Select C# Console App (.NET Framework)
2. Provide name for your project. Note: I have selected .NET Framework 4.6.2 to avoid Microsoft.Xrm library compatibility issues with the latest version of .NET Framework.
3. Once solution is created, next step is to install required library. Go to Project tab and select Manage NuGet Packages
4. Go to Browse tab in search “Microsoft.Xrm.Tooling” and select the following one “Microsoft.CrmSdk.XrmTooling.CoreAssembly” and install the package. Press OK and I Accept in the popups afterwards.
5. Next step is to go “Program.cs” in your project and add “Microsoft.Xrm.Tooling.Connector” reference
6. Write the following code in you “Program.cs” and you are good to go!
7. Build the application and you should see the following output: