Overview
CRM Developer Extensions provide an additional abstracted method of accessing the underlying CRM Organization Service providing:
- A simpler method in which a developer can connect to the Organization Service through a CrmConnection object.
- Custom extensions and context configurability through the CrmConfigurationManager.
- The ability to cache service results using the CachedOrganizationService to increase performance.
At it's most primative, for an application to connect to MS CRM 2011 using the developer extensions all that is required is a connection string, a CrmConnection instance and an OrganizationServiceContext which can be either generated using CrmSvcUtil.exe or programatically using the CrmConfigurationManager.
All related Crm Developer Extension objects live in the Microsoft.Xrm.Client namespace.
CrmSvcUtil.exe for Developer Extensions
CrmSvcUtil.exe for Developer Extensions offers the following main benefits:
- Generates statically typed entity classes for ease of use in your code.
- Generates many-to-many relationship classes to allow you to manage more complex relationships in your CRM Organization.
- Generates class names and property names based on Microsoft Dynamics CRM schema names.
- Generates a WCF Services compatible Data Context supporting OData
The data context class is generated by CrmSvcUtil.exe using the /codecustomization and /servicecontextname parameters:
CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization,Microsoft.Xrm.Client.CodeGeneration"
/url:https://crm-org-name.crm.dynamics.com/org-id /username:user-wlid-email /password:user-crm-pwd
/deviceid:user-defined-deviceid /devicepassword:user-defined-devicepwd" /out:"Xrm.cs" /namespace:Xrm
/serviceontextname: crmOrgServiceContext
Note: If no servicecontextname parameter is specified then a Data Context is not generated by CrmSvcUtil.exe
CrmConnection
At it's simplest the CrmConnection class is a wrapper that provides all of the items necessary for accessing and authenticating against the OrganizationService. To access the OrganizationService, CrmConnection provides two public constructor overloads allowing us to specify the connection settings for our CRM Organization as either a string which represents the name of the configuration stored in the web or app.config file, or as a CrmConnectionStringSettings object (System.Configuration namespace).
Bill Ryan's blog provides an excellent dive under the covers of the CrmConnection class.
In addition, CrmConnection provides a static Parse method for parsing a string URL and instantiating a CrmConnection instance:
CrmConnection Constructors
public CrmConnection(string connectionStringName);
public CrmConnection(string CrmConnectionStringSettings connectionString);
public static CrmConnection Parse (string connectionString)
Sample Connection String defined in a configuration file.
<connectionStrings>
<add name="crm" connectionString="Url=http://my.server.com/MyOrganizationName;"/>
</connectionStrings>
Use the connection string from the configuration file named "crm"
var connection = new CrmConnection("crm");
Use the ConnectionStringSettings object
ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings["crm"];
var connection = new CrmConnection(connectionSetting );
Use the CrmConnection.Parse method
var connection = CrmConnection.Parse("Url=http://my.server.com/MyOrganizationName; Domain=MyDomain; Username=crm; Password=monkey;");
OrganizationServiceContext for Developer Extensions
OrganizationServiceContext can be configured and instantiated in several ways. Crm Developer Extensions provide the ability to quickly define a CrmConnection, use the CrmConnection to create an OrganizationService instance from which we can generate an OrganizationServiceContext.
In addition, Crm Developer Extensions allows you to define and configure the context in a configuration file and surface this configuration to your application using the CrmConfigurationManager class. This section will briefly look at both of these implmentations. Additional details about the OrganizationServiceContext class can be found in my previous post here.
Instantiate OrganizationServiceContext with a CrmConnection
var service = new OrganizationService(connection);
var context = new CrmOrgServiceContext(service);
Note: CrmOrganizationServiceContext is the developer specified name for the OrganizationServiceContext which was specified as part of the /serviceContextName parameter when CrmSvcUtil.exe was used to generate the Data Context.
Configure OrganizationServiceContext in a Configuration File
The CrmConfigurationManager wraps the underlying .Net ConfigurationManager class to provide additional functionality for defining connection settings in a configuration file and using them for creating an OrganizationServiceContext instance in your applicaiton. At a high level the CreateContext method takes the name of the context configuration element that defines the settings for the data context as shown below:
var contextName = "Xrm";
using (var context = CrmConfigurationManager.CreateContext(contextName) as CrmOrgServiceContext)
{
}
Microsoft has a great link here which demonstrates how to fully configure a Data Context via the app / web.config file.