Follow the below Steps, to connect CRM Online or On-Premise using C#,
Step 1: Include the below References in your project, you can get the same from Latest SDK. Open SDK and goto the path, SDK –> Bin for the dlls
Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Sdk
Step 2: Include the below Framework Assemblies in your project,
System.Runtime.Serialization
System.ServiceModel
Step 3: Finally add the below namespaces in your class file,
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
Your References will be looking like this in Solution Explorer,
Step 4: Use the Below Method to your code for establishing CRM Connection, which consists of 3 parameters
Parameter Name | Usage |
UserName | Online: Provide office 365 userid On-Premise: Provide domain\username |
Password | Provide password |
SoapOrgServiceUri | Open Mscrm online or on-premise, Goto Microsoft Dynamics CRM -> Settings -> Customizations -> Developer Resources. Under Service Endpoints -> Organization Service. Ex: https://<OrgName>.api.crm5.dynamics.com/ XRMServices/2011/ Organization.svc |
static IOrganizationService _service; public static void ConnectToMSCRM(string UserName,string Password,string SoapOrgServiceUri) { try { ClientCredentials credentials = new ClientCredentials(); credentials.UserName.UserName = UserName; credentials.UserName.Password = Password; Uri serviceUri = new Uri(SoapOrgServiceUri); OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null); proxy.EnableProxyTypes(); _service = (IOrganizationService)proxy; } catch (Exception ex) { Console.WriteLine("Error while connecting to CRM " + ex.Message); Console.ReadKey(); } }
Step 5: For this example, I am using Console application. Finally the code looks like this,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using System.ServiceModel.Description; namespace ConnectToCRM { class Program { static IOrganizationService _service; static void Main(string[] args) { ConnectToMSCRM("arunpotti@XYZORG.onmicrosoft.com", "XYZORGPassword", "https://XYZORG.api.crm5.dynamics.com/XRMServices/2011/Organization.svc"); Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId; if (userid != Guid.Empty) { Console.WriteLine("Connection Established Successfully"); Console.ReadKey(); } } public static void ConnectToMSCRM(string UserName,string Password,string SoapOrgServiceUri) { try { ClientCredentials credentials = new ClientCredentials(); credentials.UserName.UserName = UserName; credentials.UserName.Password = Password; Uri serviceUri = new Uri(SoapOrgServiceUri); OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null); proxy.EnableProxyTypes(); _service = (IOrganizationService)proxy; } catch (Exception ex) { Console.WriteLine("Error while connecting to CRM " + ex.Message); Console.ReadKey(); } } } }
Step 6: Build the project and Click on Start, you can see the below output