Shan McArthur

As Vice President - Technology and Senior Architect of ADXSTUDIO CMS, Shan exhibits a profound dedication and proficiency in his field. Having specialized in the IT industry for over a decade, his experience and knowledge support his instrumental role in the architecture and ongoing development of the company's web development technology.
Tags
CRMUG User Group Website
Using CRM SDK Instead of Web References
January 18, 2009 8:13 PM |
Comments (0) 
| Rate this article: 

My opinion is that the best-kept secret in the CRM SDK is that you can use the SDK assemblies directly and completely avoid the problems of adding a web reference to your CRM directly into your development project.

First off, I will give a little background for some of the new CRM developers out there.  Most CRM programming examples start out by having you create a web reference to your CRM services (there are three services in total).  When you add a web reference, you have to give the web reference a name and a namespace.  What happens under the hood is that this information is used to build proxy client classes that can be used to consume the services in your client application. As a result, many of the code examples you see don't compile on their own until you add a web reference, and when you do that, you need to add it with the exact name and namespace that was used in the code for this to work properly. 

Another consequence of adding a web reference is that you now have a url to your CRM embedded directly into your code.  Nobody in their right mind will be developing against their production CRM, so in essence, you will really have a url to your development CRM instance in your code.  You have two choices at this time - you can change the code before you deploy (bad idea), or you can add an application variable to your configuration file and then substitute the url in code at runtime.  This means additional code, and each code example you find has a differnent name for the application variables, and there are more than one of them because you frequently also have to provide credentials as well.  Finally, none of this works for CRM Online, nor for SPLA deployments.  You will find that most CRM examples out there will only work for on-prem deployments, which is really not that ideal.

Another issue with using a web reference is that the web reference to your CRM is a dynamic web reference.  Microsoft has decided to model out your CRM entities and customizations in the web reference to give you some classes to work with.  For example, you will see a contact class with all the attributes in your contact entity.  At first, this looks pretty convenient, but consider that the service wsdl changes every time a business user customizes the CRM and hits the publish button.  What you are left with is a dynamic contract.  Contracts are supposed to be fixed in my opinion.  It is too fragile to have a dynamic service reference that can cause runtime errors in your existing applications just because someone customized the CRM by cleaning up an unused field or adding another one.

Microsoft has a better way for you to use the CRM.  They have included assemblies (microsoft.crm.sdk.dll and microsoft.crm.sdktypeproxy.dll) in the CRM SDK.  These DLLs have everything you need to fully utilize the CRM.  When you use these DLLs, there is no reason you have to add a web reference to your project.  They are so easy to use that I am surprised that not all examples are written to use them.  It certainly would make for a better getting-started scenario as all examples could compile and run without any code changes.

The most significant difference you will find is that you will have to use the dynamic entity when you work with your CRM entities.  I view that as a good thing (check out my other blog article).

When you are developing plugins, you are forced to use the assemblies in the SDK, so eventually you will find that you have to know how to use them anyways, so in my opinion, it is better to use them everywhere and get rid of all the problems of using a web reference.

When you start to work more with the CRM SDK, you will eventually use some form of connection or configuration library, and any reusable library will need to use the SDK dlls as well instead of your web references, especially if they are compiled into a seperate dll.

How to use the SDK web services

The microsoft.crm.sdktypeproxy dll contains the client proxy classes that you need to use the CRM.  Using the client proxy in this DLL is very simple.  Just add a reference to your project to the assembly, add an appropriate using statement in your code, and new up an instance of the CrmService class.  This class will require some parameters before you use it, but then if you have to parameterize a web service in your project, they are the same parameters that need to be set.  In this case, you simply set the CrmAuthenticationTokenValue, Url, and Credentials properties to the appropriate values for your site, and set the PreAuthenticate property to true.  Once you set these parameters, you can use it like your normal service.

The only caveat is that you should not use all the classes that are specific to entity types (ie: contact, lead, opportunity, etc.)  You can (and should) use Dynamic Entities for all CRM operations.

Most Convenient way to use the CRM Services

The easiest way to use the CRM Services and connect to all CRm deployment models is to use the ADXSTUDIO CRM Developer Toolkit.  This allows you to place the CRM connection details in a connection string in your config file and simply new up a new CrmConnection and call CreateCrmService() or CreateMetadataService() to obtain handles on fully-authenticated and initialized web services.

Links & Attachments 
Submit a Comment
Title:  
Name:    
Comment:    
Verification:

Type the characters you see in the picture below.