How to Connect to Dynamics CRM Online
This walkthrough shows you how you can use XrmLinq to connect to Dynamics CRM Online. XrmLinq allows you to use the same codebase (Entity Mapper generated code) against Dynamics CRM Online, On-Premise and Partner Hosted versions. This gives you the ability to port your xRM solutions with simple configuration changes.
Requirements
- XrmLinq 1.5.3 or above
- Access to Dynamics CRM Online
- Visual Studio, .NET 3.5
- CRM SDK 4.0.11 or above
Creating a Visual Studio Solution
To begin, you need to create a project for your code. For this example we will be creating a simple console application and retrieving accounts from your Dynamics CRM Online instance.
- Open Microsoft Visual Studio
- Create a new console application project (preferably C#)
Adding the required references
- Right click the project references, click on Add Reference and add the reference to the following dlls
- Microsoft.Crm.Sdk
- Microsoft.Crm.SdkTypeProxy
- System.Runtime.Serialization (this is required if the generated code is WCF compatible)
- System.Web.Services
- XrmLinq
Generating the code
- Sdk Url: https://dev.crm.dynamics.com/mscrmservices/2007/Passport/CrmDiscoveryService.asmx
- Organization Name: Friendly name of your Dynamics CRM Online instance
- User Name: Your Microsoft Passport login which is used to login to the Dynamics CRM Online instance
- Partner: crm.dynamics.com
- Environment: Production
- Click on Connect
- Specify the Output File Path and click Generate
Generate will take a few minutes (between 5-10 minutes depending on your connection speed), the main delay is getting the metadata back from the Dynamics CRM Online API server.
Let’s write some code
- Switch back to your Visual Studio project
- Right click on the project and click on Add -> Existing Item -> Browse to the file path you specified at step 7.
- Build the project (Shift + Ctrl + B or Right click on the project -> Build) – the project should build without errors
- Copy paste the code below into the Main() method
// create a connection to the dynamics crm online server
CrmService sdk = Connection.Create(
"https://dev.crm.dynamics.com/mscrmservices/2007/Passport/CrmDiscoveryService.asmx",
"XrmLinq", "support@xrmlinq.com", "***", "crm.dynamics.com", "Production");
// data context to access dynamics crm online
CrmDataContext ctx = new CrmDataContext(sdk);
// simple query to get account names
var accounts = (from a in ctx.Accounts select a.Name).ToList();
foreach (var account in accounts)
{
Console.WriteLine(account);
}
Run the application to see the account names being written to the console screen. The same code can now be used on the On-Premise and Partner Hosted versions simply by calling the overloaded constructor of the Connection.Create method.





