Sample code to connect to CDS / Dynamics 365 / CE using OAuth

Sending
User Review
0 (0 votes)

Sample code for quick reference for connecting to CDS through a console application using OAuth

Add the NuGet package for Microsoft.CrmSdk.XrmTooling.CoreAssembly in the project

Note

Here we will be using the sample AppId and Redirect URI.

Sample Code- 

 using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System; namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = "AuthType = OAuth; " + "Username = [username]@[domain].onmicrosoft.com;" + "Password = [password]; " + "Url = https://[orgname].crm.dynamics.com;" + "AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;" + "RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" + "LoginPrompt=Auto"; CrmServiceClient svc = new CrmServiceClient(ConnectionString); if (svc.IsReady)
{
var myContact = new Entity("contact");
myContact.Attributes["lastname"] = "Test";
myContact.Attributes["firstname"] = "User1";
svc.Create(myContact);
}
}
}
} 

More details on – Connection string parameters.

Hope it helps..