共用方式為


CRM 4 and Silverlight 4 Communicating together

When using Silvelright 4 with CRM 4, we need to keep in mind that the CRM SOAP services are a different version of SOAP as supported by Silverlight.

The main things we need to keep in mind are:

1. Adding a message header explicitly into the service proxy so that it takes the "Authentication token". Without the authentication token you wouldn;t be able to authenticate users to access CRM from Silverlight.

Snippet:

On page_load:

MessageHeader header = MessageHeader.CreateHeader("CrmAuthenticationToken", "https://schemas.microsoft.com/crm/2007/WebServices", "", new TheCrmAuthenticationTokenSerializer(0, "MyCrmOrganization", null));
OperationContext.Current.OutgoingMessageHeaders.Add(header);
crmService.RetrieveAsync("contact", new Guid("5GT12C70-RA34-IT11-VH29-97532BG22J76"), columnlist);

public class TheCrmAuthenticationTokenSerializer : XmlObjectSerializer
{
int _authType;
string _organizationName;
string _callerId;

public MyCrmAuthenticationTokenSerializer(int authType, string organizationName, string callerId)
{
_authType = authType;
_organizationName = organizationName;
_callerId = callerId;
if (_callerId == null)_callerId = "00000000-0000-0000-0000-000000000000";
}

public override void WriteStartObject(XmlDictionaryWriter writer, Object graph)
{}

public override void WriteObjectContent(XmlDictionaryWriter writer, Object graph)
{
string authToken = string.Format("<AuthenticationType xmlns='https://schemas.microsoft.com/crm/2007/CoreTypes'>{0}</AuthenticationType><OrganizationName xmlns='https://schemas.microsoft.com/crm/2007/CoreTypes'>{1}</OrganizationName><CallerId xmlns='https://schemas.microsoft.com/crm/2007/CoreTypes'>{2}</CallerId>", _authType, _organizationName, _callerId);
writer.WriteRaw(authToken);
}

public override void WriteEndObject(XmlDictionaryWriter writer)
{}

public override bool IsStartObject(XmlDictionaryReader reader)
{
return true;
}
public override Object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{return null; }
}

 

 

2. Keeping all the methods asynchronous since Silverlight only does async calls

These are the main things you need to keep in mind. 

CRM 2011 would eventually be coming up with an easier way to implement Silverlight so keep watching for it and upgrade to the newer CRM :)

Enjoy!