Tuesday, March 29, 2011

Connecting to CRM 2011 using connectionStrings

One of the nice features we have in the Advanced Developer Extensions for CRM4 (since sdk v4.0.12) was the ability to define the connection details inside the connectionStrings section in the web.config file. However, this facility was missing in the early versions of the CRM2011 SDK. However, the latest update of the CRM SDK (March) has finally included this feature.  In fact, this is one of many extended set of features known as Developer Extensions (note the missing "Advanced" word from CRM4).

For instance, suppose you need to connect to a CRM 2011 server using Integrated security from a custom web project. First you have to define the CRM connection details inside the web.config (or app.config for other project types) as follows;

image
 

If you need to use Active Directory authentication, then you may include the user account details in the connectionString as follows.

image

For CRM Online we have DeviceID and DevicePassword parameters instead of Domain.

Once you defined the connection details, you can connect to the server as follows;

image

Please note that these new classes are part of a new assembly Microsoft.Xrm.Client.dll. So you need to add this assembly to your project as a reference.

Alternatively, you may also use the value you usually include inside the connectionString directly in the code. This method is particularly useful if your connection details are not static (i.e. you dynamically get the values). The bellow code segment shows how you can create a CrmConnection by using this method.

image

No comments:

Post a Comment