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
I think this is one of the most vitql info for me. And i am
glad reading your article. But wamna remark on some general
things, The website style is ideal, the articles is reeally excellent
: D. Good job, cheers
LikeLike
Thank you jimmy 🙂
LikeLike
superb arun ur awesome,try for mvp also posting in crm community….u worth it
LikeLiked by 1 person
Thank you Mani deep. Sure will do that.
LikeLike
Thank you so much for this post 🙂
LikeLike
Thank you
LikeLike
[…] https://arunpotti.wordpress.com/2014/12/09/connect-to-crm-online-or-on-premise-using-c/ […]
LikeLike
Thanks a lot for such a explainatory post 🙂
LikeLike
I am getting some “Metadata contains a reference that cannot be resolved”…..Please help
LikeLike
Hi Yamini,
Please Check whether Organization Service URL, Userid, Domain and Password are correct or not.
LikeLike
i am getting similar error.
{“Metadata contains a reference that cannot be resolved: ‘https://XXXXX.crm4.dynamics.com/XRMServices/2011/organization.svc?wsdl&sdkversion=8.1’.”}
i checked username password thrice.
please help me.
LikeLike
Hi Pushkaraj,
Please check the below link for my post on Dynamics 365,
https://arunpotti.wordpress.com/2018/02/03/step-by-step-to-connect-dynamics-365-crm-online-v9-x-using-c-console-application/
LikeLike
Instruction is crystal clear! great post.
LikeLike
Thank you Patric
LikeLike
Hi Arun,
Thanks for this explainatory post.
I am trying this but in WebResource ,it is showing the type or namespace name WebResource coulod not be found( are You missing using directive or namespace reference)
LikeLike
Thank you Kundan.
Please explain me the steps, that you have done? Will try to help you out.
LikeLike
Thanks a lot. You ve been ery helpful : )
LikeLike
Thank you 🙂
LikeLike
Hi there! I could have sworn I’ve visited this blog before but after browsing through a few of the posts I realized it’s new to
me. Nonetheless, I’m definitely delighted I
came across it and I’ll be bookmarking it and checking back frequently!
LikeLike
We’re a group of volunteers and opening a new scheme in our community.
Your site offered us with valuable information to work on. You’ve done an impressive
job and our entire community will be grateful
to you.
LikeLike
I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your website to come back in the future.
Many thanks
LikeLike
I’ve read several good stuff here. Definitely value bookmarking for revisiting.
I wonder how much attempt you put to make this sort of wonderful informative
site.
LikeLike
Arun,
Thank you for this amazing article. For a Noob CRM developer, the MS Documentation and SDK examples are completely unfathomable! I couldn’t get the SDK sample programs to connect to my CRM org, and deciphering the thousands of lines of code was impossible. With your clear description of how to set up the references and assemblies, I got connected to CRM in five minutes 🙂
Thanks,
Tony
LikeLike
Thank you!. It worked for me to connect on-premise CRM (8.1)
LikeLike