MSCRM Advanced Developer Extensions – LINQ

The new MSCRM SDK (4.0.12) contains a range of changes to how MSCRM Development will be undertaken, including the addition of a Code Compiler that at early glance looks to take the place of the previous paradigm of adding a Web Reference to the CRM Web Service into Visual Studio.

The Code Compiler itself does not actually do much different from action of adding a Web Reference in that you point the compiler to the MSCRM Deployment in question (whether this be Internet-facing, Live or On-Premise) and generate the code that can be referenced in your MSCRM Extension code to add as a Data Layer connection to the MSCRM Deployment – however this now includes support for LINQ.

I work primarily as a Functional Consultant, and so am not up to date with most recent Development Practises outside of core MSCRM, SQL and SharePoint development which means that LINQ is quite new to me. However with LINQ coming into MSCRM with the most recent version of the SDK – now would be a good time to get myself up to date.

Examining this, the following is a simple guide to compiling and using the new SDK Tools to create a simple .NET Console Application to pull information from MSCRM via a LINQ Query.

STEP 1 – Our first step is to create a new .NET Console Application in Visual Studio 2008 – here we then need to add a App.Config to our project via adding an Application Configuration file. Within this Configuration, we will then add a Connection String Key to specify how we will communicate to our MSCRM Deployment:

<?xml version="1.0" encoding="utf-8" ?>
    <add name="Crm" connectionString="Authentication Type=AD; Server=http://localhost:5555/PYDEV; User ID=PYDEV\administrator; Password=somepassword;"/>

The example given above details a connection to an Active Directory (i.e. On-Premise) Deployment of MSCRM where the Domain and Organisation Name is both ‘PYDEV’ and our connection user account is ‘administrator’ – and using ‘localhost’ as our HTTP address for MSCRM with a Port Number of the MSCRM Default of 5555.

STEP 2 – Secondly we need to run the Code Generation Tool provided with v4.0.12 of the MSCRM SDK to produce a code file for use in our project.

Within the SDK Download, the Code Generation Tool can be found at ‘.\sdk\microsoft.xrm\tools\CrmSrvUtil.exe’

We then need to run this tool with our MSCRM Connection string supplied as a command line parameter from a Command Prompt, using the example above this would be:

crmsvcutil /connectionString:"Authentication Type=AD; Server=http://localhost/UKCRM; User ID=PYDEV\administrator; Password=somepassword;" /out:"Xrm.cs" /namespace:Xrm /dataContextPrefix:Xrm

Running this command in a Command Prompt will result in a screen similar to the following:

Running CrmSvcUtil

Command Prompt showing result of running the CrmSvcUtil Tool

This leaves us with a new ‘Xrm.cs’ code file to include in our .NET Project.

STEP 3 – We must then add this new ‘Xrm.cs’ code file into our .NET Project.

For this code to function within our Project, we must include the following DLLs as References:

Microsoft.Crm.SDK Supplied as one of the core BIN files in all versions of the MSCRM SDK
Microsoft.Xrm.Client Supplied as one of the new Advanced Developer Extension BIN files in the MSCRM SDK v4.0.12 – found in ‘.\sdk\microsoft.xrm\bin’
Microsoft.Xrm.Portal Supplied as one of the new Advanced Developer Extension BIN files in the MSCRM SDK v4.0.12 – found in ‘.\sdk\microsoft.xrm\bin’
System.Data.Services One of the .NET Standard References for incorporating Web Services into a .NET Project.
System.Data.Services.Client Additional Standard Reference for incorporating Web Services into a .NET Project, required to support the generated ‘Xrm.cs’ code.

These then sit alongside the standard References for a new .NET Project – once added, the .NET Project should look similar to the following screen impression:

LINQ Hello World CRM - References

Example .NET Project to show references required for using LINQ with MSCRM

STEP 4 – With these pre-requisite steps completed, we can now add some code to our Program.cs Main function to run a LINQ query against our MSCRM Deployment and produce a result. The code shown below gives a very basic example of this, in retrieving all the Contacts based in ‘London’ and listing them on-screen as the Console Application’s Output:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm;

namespace LinqHelloWorldCRM
    class Program
        static void Main(string[] args)
            // create LINQ connection from 'Crm' Connection String held in App.Config
            var crm = new Xrm.XrmDataContext("Crm");

            // get certain subset of Contacts from MSCRM as a list
            var mscrmContacts =
                from c in crm.contacts
                where c.address1_city == "London"
                select c;

            // cycle list of Contacts and display each Contact's Fullname and Email Address
            foreach (var c in mscrmContacts)
                System.Console.WriteLine(c.fullname + " " + c.emailaddress1);

STEP 5 – Compile and run the project to then create our first LINQ Application for MSCRM, the output is obviously pretty basic resulting in a screen similar to the following:

LINQ Hello World CRM - Project Output

The Console Application output produced from our simple LINQ CRM Project

Very basic – and requiring a few setup steps, references and code files to undertake. However the ability to query CRM so quickly without constructing the usual QueryExpression, ConditionExpression and FilterExpression objects does appear powerful – particularly when we look at what else is possible through the example code provided in the SDK:

//Create a new contact called Allison Brown.
var contact = new
  firstname = "Allison",
  lastname = "Brown",
  address1_line1 = "23 Market St.",
  address1_city = "Sammamish",
  address1_stateorprovince = "MT",
  address1_postalcode = "99999",
  telephone1 = "12345678"

Much quicker than traditional MSCRM Development Webservice calls, and an ability to support Transaction Management through the .SaveChanges element.

As mentioned, my experience with LINQ so far is quite limited but I imagine the tie-up with traditional SQL and the use of a common Data Access Language will have significant benefits for future MSCRM Development.

And with these Developer Extensions coming late in the cycle for MSCRM v4, these extensions to take MSCRM into the world of LINQ may be an indication of where Microsoft is taking development of the product in the future.

This entry was posted in CRM 2011, LINQ, MSCRM, Technical. Bookmark the permalink.

2 Responses to MSCRM Advanced Developer Extensions – LINQ

  1. Dan says:

    Thanks for the sample codes!

  2. Nice article… dont forget Verison 4.0.13 of the SDK is out now, with some nice additional helper classes. We used your tips on one of our client’s Gifts Web Site to integration with their CRM package. Thanks very much!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s