Friday, August 6, 2010

MS CRM 4.0 LINQ Support in CRM SDK 4.0.12

So far I really like what I have seen in this new SDK release MS CRM SDK.

It solves real problems and will actually make life better for CRM developers.

The new LINQ support can be used by people customizing their own propriety CRM systems and for integrators and xRM developers with software to deploy across different CRM schemas.

How they got it right and made life easier!

  1. The attributes can be directly accessed as simple types!
    • bool?  instead of  CrmBoolean
    • Guid   instead of   Key
    • etc..
  2. The related entities are associated as a list of entities like you would expect with LINQ
    • IEnumerable<contact>  instead of a BusinessEntityCollection.
    • Many less queries need to be coded greatly simplifying code.
      • Even though the additional queries are generated internally, they are done on the SQL side rather then requiring multiple web service calls which should be faster.
    • The related entity list names are predictable!
      • ex.  acct.account_contacts
      • ex.  acct.new_account_contact
    • Note: This is a significant improvement from the LINQtoSQL model that would just name related entities  contacts, contacts1, contacts2 without any way to predict which related list is which and wouldn’t give you a permanent way to set naming behavior. Adding an additional relationship to an entity that was already related could ruin your existing LINQ, requiring manual editing of the model xml.
  3. There is now much less of a reason to need to use the much more primitive Fetch type syntax.  string xmlResults = crmService.Fetch(xmlQuery);
  4. Entity classes do NOT need to be identical to the CRM schema for Selects!
  5. Entity classes do NOT need to be identical to the CRM schema for Updates!
    • According to MS the Update operation sends only modified attributes back to the server so you don’t have to care about anything else that’s been added.

In a nutshell

Strong typing and compile time errors
No web reference required
Easily regenerated Entity classes

Simple Maintenance

From a command line all you have to do is use the new crmsvcutil.exe to generate and update the LINQ Entity class source files. More information here.  MSDN reference

C:\> crmsvcutil /server:http://crm/Contoso /out:MyLinqEntities

One of the reasons I really appreciate this is that I wrote a utility to read all of the MS CRM Metadata and generate (Data Transfer Objects) or DTO’s for RIA services about a year ago along with all of the associated attributes for validation.  Idea for MS: If we had a switch on this utility to dynamically generate validation criteria attributes from the MS CRM metadata so that we could bind a control to it, that would be great for MVVM Silverlight apps!  Or generate them by default and just comment them out.

Getting Started:

David Yack has an explanation of how to set up MS CRM LINQ on your system and use it in a hello world project. There is no point duplicating his effort.

Sample Solution:

The following is a very simple sample solution 7.5Mb that is little more than the sample in the explanation link above. However the SDK libraries are included in the solution as are the Entity class files generated from the stock MS CRM 4.0 Contoso VM with a couple additions to the Account entity.

Since it ONLY accesses a couple standard fields in the Account entity it should work out of the box on any CRM 4 box once the Server name is set in the app.config file.  Just change the line below to whatever your local server name/orgname is and it should work.

Server=http://crm/Contoso;"/>

This is good stuff and the sooner you start using this the more time it will save you!