Skip to content

Instantly share code, notes, and snippets.

@naveedahmed986
Last active December 29, 2020 11:21
Show Gist options
  • Save naveedahmed986/f08c863833d607d948292520423fd3de to your computer and use it in GitHub Desktop.
Save naveedahmed986/f08c863833d607d948292520423fd3de to your computer and use it in GitHub Desktop.
Dynamics365 Online Connection in C# Console Application
using System;
using Microsoft.Xrm.Tooling.Connector;
namespace Dynamics365_Console_Application
{
class Program
{
static void Main(string[] args)
{
try
{
string authType = "AuthType=OAuth"; // AuthType is OAuth and not Office365
string userName = "<username>@<domain>.onmicrosoft.com"; // Add your username here
string password = "<password>"; // Add your password here
string domainURL = "https://<domain>.crm4.dynamics.com"; // Add your Dynamics365 instance URL here
string universalCodes = "appid=51f81489-12ee-4a9e-aaae-a2591f45987d;redirecturi=https://callbackurl"; // Universal Id and uri
string connectionString = string.Format("{0}{1}", authType, ";") +
string.Format("Username={0}{1}", userName, ";") +
string.Format("Password={0}{1}", password, ";") +
string.Format("Url={0}{1}", domainURL, ";") +
universalCodes;
Console.WriteLine("*** Establishing connection to CRM... ***");
CrmServiceClient client = new CrmServiceClient(connectionString);
if (!client.IsReady)
{
Console.WriteLine(client.LastCrmException.Message);
Console.WriteLine(client.LastCrmException.Source);
Console.WriteLine(client.LastCrmException.StackTrace);
}
else
{
Console.WriteLine("\n*** Connection Successful! ***");
// Perform client operations here...
}
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey(true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment