console application c# dynamics crm code example
Example: console application in c# ms crm
IOrganizationService organizationService = null;
try
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>.onmicrosoft.com";
clientCredentials.UserName.Password = "<ProvideYourPassword>";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<ProvideYourOrgName>.api.<CRMRegion>.dynamics.com/XRMServices/2011/Organization.svc"),
null, clientCredentials, null);
if (organizationService != null)
{
Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
Console.WriteLine("Connection Established Successfully...");
}
}
else
{
Console.WriteLine("Failed to Established Connection!!!");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught - " + ex.Message);
}
Console.ReadKey();