Most of the time we need to have custom functionality that improves the efficiency of the product. Console application is one of them.
Let us see how we can Connect Dynamics 365 from the Console Application.
To achieve this, we need to have a Visual Studio setup on our computer.
Use Case:
Here we will see how we can connect to Dynamics 365 from the Console Application.
Follow the below mentioned steps:
- Open Visual Studio.
- Create a new solution in the visual studio with a console application.
- It requires assemblies of SDK and you can install using the Manage NuGet package. Click on Manage Nuget Package and search for “crmsdk.Coreassemblies” and “Microsoft.CrmSdk.XrmTooling.CoreAss” and then select the latest version and click on install for each of them.
- Add the code below in your code. Update your username and password and org.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// additional namespaces
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Tooling.Connector;
using System.Net;
using Microsoft.Xrm.Sdk;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// defining credentials
//Provide your credentials here
string url = “Organization URL”;
string userName = “User name of your organization”;
string password = “Your organization password”;
//Connection string
string conn = $@”
Url = {url};
AuthType = OAuth;
UserName = {userName};
Password = {password};
AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;
LoginPrompt=Auto;
RequireNewInstance = True”;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (var svc = new CrmServiceClient(conn))
{
WhoAmIRequest request = new WhoAmIRequest();
WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request);
Console.WriteLine(“Your UserId is {0}”,response.UserId);
Console.WriteLine(“Press any key to exit.”);
Console.ReadLine();
}
}
}
} - Save and Build the application and make sure that no errors are there in the application.
- Click on “Start” from the top to execute the code. If it shows you the window below then your application runs successfully.