Wednesday, February 28, 2007

Callouts in Service Provider Edition of Microsoft CRM 3

Finally!
I got it to work, and it actually wasn't too difficult when I got hold of the correct DLL:s. So, first hot tip, right out the furnace:

"When developing for CRM Service Provider Edition, make sure you got CRM Service Provider Edition versions of your dll:s".

This can actually be a bit problematic when working in a development environment on a VPC since it is quite rare to have the entire Hosted Environment, like Hosted AD, Hosted Exchange etc on your virtal machines. If you do, great, use it, but I would guess that most of you don't.

I will give you a short walkthrough of how to create a post callout using the CRM Service Provider Edition. I will focus mainly on the differences between developing a normal callout and one for for CRM SPE.

First off, copy the file Microsoft.Crm.Platform.Callout.Base.dll from the CRM SPE CD 1 (\bin\Assembly). Put it somewhere where it is possible to add a reference to it. Like c:\temp.
Now, add a reference to it in the project.

Modify the callout.config.xml file just like you usually do, for instance:
<callout.config version="3.0" xmlns=" http://schemas.microsoft.com/crm/2006/callout/">
<callout entity="account" event="PostUpdate">
<subscription assembly="CalloutLibrary.dll" class="CalloutLibrary.AcctCallouts" />
</callout>
</callout.config>

Create a class file called AcctCallouts in the namespace CalloutLibrary and make sure the dll is called "CalloutLibrary.dll". (If you name your project CalloutLibrary, this will be default).

using System;using Microsoft.Crm.Callout;
using System;using System.Data;
using System.Net;
using System.Web;
using MSAB.CalloutLibrary.CRMSDK;
using Microsoft.Crm.Callout;
using System.Globalization;
using System.IO;


namespace CalloutLibrary
{
public class mbaktivitet: CrmCalloutBase
{
private CrmService service;
private WhoAmIRequest userRequest;
private WhoAmIResponse user;

public override void PostCreate(
CalloutUserContext userContext,
CalloutEntityContext entityContext,
string postImageEntityXml)
{
string usr = "
username@test.local";
string pwd = "1m0rePassword";
string HostedCRMurl ="http://crmserver.hostingcompany.com";
service = new CrmService();
service.Url = HostedCRMurl + "/mscrmservices/2006/crmservice.asmx";
service.CookieContainer = new CookieContainer();

service.CookieContainer.Add(GetCRMCookie(HostedCRMurl, usr, pwd));

//Make sure the webservice works
userRequest = new WhoAmIRequest();
user = (WhoAmIResponse) service.Execute(userRequest);
}
}
}

The important call here is the GetCRMCookie(HostedCRMurl, usr, pwd), a method that I will describe bellow which returns an authentication cookie that can be used to authenticate against the web service. Here is its definition, it is more or less what Arash has written on his blog concerning how to write web services for CRM SPE.

public Cookie GetCRMCookie(string url, string userName, string password)
{
DebugText("Start of GetCRMCookie");
Cookie authCookie;
try
{
Uri serverUri = new Uri(url);
Uri logonServerUri = new Uri(serverUri, @"LogonServer/Logon.aspx");

string encodedUserName = HttpUtility.UrlEncode(userName);
string encodedPassword = HttpUtility.UrlEncode(password);
string logonServerUrl = String.Format(CultureInfo.InvariantCulture, "{0}?UserName={1}&Password={2}", logonServerUri.ToString(), encodedUserName, encodedPassword);

// Make a web request that does not allow redirection
HttpWebRequest logonRequest = (HttpWebRequest)WebRequest.Create(logonServerUrl);
logonRequest.AllowAutoRedirect = false;
logonRequest.CookieContainer = new CookieContainer();
HttpWebResponse logonResponse = (HttpWebResponse)logonRequest.GetResponse();
authCookie = null;

using (logonResponse)
{
if (HttpStatusCode.Found != logonResponse.StatusCode)
{
// throw new CrmException(logonServerUrl, ErrorCodes.InvalidOperation);
}

if (null == (authCookie = logonResponse.Cookies["CRMAuthCookie"]))
{
// throw new CrmException(logonServerUrl, ErrorCodes.InvalidOperation);
}
}

// Now we have the cookie, make sure the credential is valid by passing the cookie back to CRM
logonRequest = (HttpWebRequest)WebRequest.Create(serverUri);

// The following user agent is required by CRM
logonRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";
logonRequest.AllowAutoRedirect = false;
logonRequest.CookieContainer = new CookieContainer();
logonRequest.CookieContainer.Add(authCookie);

logonResponse = (HttpWebResponse)logonRequest.GetResponse();

using (logonResponse)
{
if (HttpStatusCode.OK != logonResponse.StatusCode)
{
throw new InvalidOperationException(logonServerUrl);
}
}
return (authCookie);
}
catch (Exception err)
{
return null;
}
}

That’s about it. Using this technique, you should be able to get your SPE Callouts to work.

Make sure that the callout.base is exists in the servers GAC, if not, copy it there.

Note that post-callouts, won’t break the execution if there is an error but they will leave the error exception sprecification in the eventlog so make sure you have a look at it.

So, in short, there are two main differences when developing callouts that use web services when comparing normal CRM with SPE CRM, they are:

- DLL:s are unique. The version number for SPE is 3.0.5745.0 while the version for the normal CRM is: 3.0.5300.0.

- To authenticate, you cannot use the normal System.Net.CredentialCache.DefaultCredentials but, you have to use the “slightly” more complicated method of capturing the authentication cookie and using it to log in.

As you might have noticed in the code, the user is hard-coded, which means that there will not be any impersonation in the way it works in normal CRM. I have an idea of how this might be solved; by creating a new aspx-page to capture the username and password, creating a cookie using these credentials and then storing it in the session variable to enable further loggin in later with the webservice. One would also have to make sure that the cookie can be used by the normal IE klient as well. As you might understand, there are some question marks here that I will try to straighten out. If I find a solution to impersonation in CRM SPE, I will be sure to create a post about it!


Gustaf Westerlund
CRM and SharePoint Consultant

Humandata AB
www.humandata.se

No comments:

Post a Comment