Saturday, March 5, 2011

Connecting to CRM 2011 in the Cloud

Connecting to CRM 2011 in the Cloud is trickier than connecting to CRM 2011 On Premise.
There is a great article by Deepak Kumar but it is worth recapping on his points.
Check c:\Program Files to see if you have Windows Identity Foundation. If not you will need to install it.

You need to get some device credentials and this is done using a utility in the CRMSDK called DEVICEREGISTRATION. Go to \sdk\tools\deviceregistration directory and load deviceregistration.csproj. Compile the application and navigate to the \bin\debug folder. Open a command window and type

deviceregistration.exe /operation:Register

The Device ID and Password that are generated are used in the method GetDeviceCredentials (that comes next).

Preparation work done you can now create your VS 2010 project, in this example I am using a WCF Service (I left the default name of Service)

Copy the CRMServiceHelper.cs file from \sdk\samplecode\cs\helpercode into your project and modify three methods:

GetDeviceCredentials
GetUserLogonCredentials
GetServerConfiguration

I used the code in Deepak Kumar's blog and set my Windows LiveID credentials in the GetUserLogonCredentials method. When you set the endpoints in GetServerConfiguration be aware that endpoints vary depending where you are in the world so see this blog .

Then if you want Intellisense on your custom entities and attributes you need to generate a class with them all in using CRMSVCUTIL.EXE. This tool is in the SDK too in the \sdk\bin directory. Amend your Environment Path so you have a reference to its location. Open a Command window, navigate to where you want the class to be generated then use

CrmSvcUtil.exe /url:https://{organisation}.crm4.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:"windows live id" /password:"live id password" /deviceid:"deviceid" /devicepassword:"device password"

Add the class you just generated to your project. OK, this takes a while and you will have to keep regenerating it every time you add an attribute but think of the time you'll save by having fewer bugs.

You will need to add references to the CRM SDK dlls (microsoft.crm.sdk.proxy.dll, microsoft.xrm.sdk.dll and microsoft.crm.sdk.dll) and then add these using statements

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;


Add 3 declarations in the class Service.
private OrganizationServiceContext context;
private OrganizationServiceProxy proxy;
private IOrganizationService service;


In the class constructor add a call to ConsumeIOrganization().

public Service()
{
ConsumeIOrganization();
}
Then add two methods

public void ConsumeIOrganization()
{
ServerConnection serverConnection = new ServerConnection();
ServerConnection.Configuration serverConfig = serverConnection.GetServerConfiguration();
Connect(serverConfig);
}

public void Connect(ServerConnection.Configuration serverConfig)
{
try
{
using (proxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
serverConfig.Credentials, serverConfig.DeviceCredentials))
{
proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
context = new OrganizationServiceContext(proxy);
service = (IOrganizationService)proxy;
}
}
catch (FaultException <microsoft.xrm.sdk.organizationservicefault>)
{
throw;
}
}

Now you can add your code and reference service or context methods depending on which you prefer. Welcome to programming in the cloud!

4 comments:

joon said...

why do we need to install Windows Identity Foundation on the machine from where we are going to access CRM online 2011?? I assume, it can be done without WIF. Please clarify.

regards
joon

joon said...
This comment has been removed by the author.
Unknown said...

Hi Charles,

Thanks for clearing up where to get the devicecreation.exe executable file.. Looked all over the place, for a non-techie your comments were of great value! Aad

Unknown said...

Any idea on the best approach for keeping the connection alive ie connection pooling, each page is so far reconnection to the cloud which is just too long for the end user.