Last active
December 29, 2020 11:21
-
-
Save naveedahmed986/f08c863833d607d948292520423fd3de to your computer and use it in GitHub Desktop.
Dynamics365 Online Connection in C# Console Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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