Here is a sample console application, to get connected with MS CRM / D365
Make sure, you are using .Net 4.6 framework. In case you are using .Net Framework below 4.6, please uncomment ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; in the below code. The main purpose of this to provide more secure connectivity to the application, in which we have Microsoft updated and industry standard security policies.
For more information on this, please refer here
Steps:
1. Open Visual Studio, click on New Project2. Select Visual C# and chose Console Application in the right pane.
3. Chose a desired name and location to the application
4. Go to Tools -> NuGet Package Manager -> Package Manager Console
5. In the console window, paste this - Install-Package Microsoft.CrmSdk.CoreTools -Version 9.0.2.6
Earlier, we used to add all the required CRM dlls from CRM SDK, now this the above NuGet package is taking care of that and it contains official SDK tools authorized by Microsoft.
Code:
using System;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
namespace CRMConsoleApp
{
/// <summary>
/// Program Class
/// </summary>
public class Program
{
/// <summary>
/// Main method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
IOrganizationService organizationService = ConnectCRM();
if (organizationService != null)
{
Guid currentUserGuid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
if (currentUserGuid != Guid.Empty)
{
Console.WriteLine("User GUID: " + currentUserGuid.ToString());
}
}
}
/// <summary>
/// Connecting to CRM
/// </summary>
/// <returns>IOrganization Service</returns>
public static IOrganizationService ConnectCRM()
{
IOrganizationService organizationService = null;
String username = "******@******.onmicrosoft.com";
String password = "******";
try
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = username;
clientCredentials.UserName.Password = password;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
organizationService = (IOrganizationService) new OrganizationServiceProxy(new Uri("https://******.crm.dynamics.com/XRMServices/2011/Organization.svc"), null, clientCredentials, null);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught - " + ex.Message);
Console.ReadLine();
}
return organizationService;
}
}
}