8 Apr 2011

SharePoint and CRM 2011 Integration: Part 1

by Dave

We have been looking at integrating our CRM 2011 installation with SharePoint, since we don't want to keep duplicating everything.  To do this, it seemed best to use the Business Connectivity Services (BCS) in SharePoint 2010.  However, connecting SharePoint BCS to CRM isn't as simple as pointing it to the CRM web services (which I had hoped it would be).  Instead, you have to create .net assembly connectors to connect to CRM and perform the CRUD operations.

This means a lot of custom code; one class for each CRM entity, and then a corresponding service class to perform the CRUD operations.  The XML definition also needs to be created for all the entities that you want to connect to.  To get around this, I have created a little console application which spiders through CRM, dumps all the entity information as XML, and uses XSLT to transform it into the relevant C# code and XML definition.

I will go through some of the more interesting bits of code below, namely getting the entity data from CRM, and the generated service code.

Enumerating CRM Entities

The code below uses some of the bits from the CRM SDK to get the CRM server configuration; since this is part of a console app, it makes sense.  So, in order to enumerate all of our CRM entities, we connect to the Organisation (spelt the American way...) service and then create a RetrieveAllEntitiesRequest object:

// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(config.OrganizationUri, config.HomeRealmUri, config.Credentials, config.DeviceCredentials))
{
    // This statement is required to enable early-bound type support.
    serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

    RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
    {
        EntityFilters = EntityFilters.All,
        RetrieveAsIfPublished = true
    };

This then gets all of our entities, including their attributes, as defined by the EntityFilters property. We then go on to execute the request, and get all of our entities back:

// Retrieve the MetaData.
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)serviceProxy.Execute(request);

The next thing we do is loop through each of these entities and dump out XML for them so that we can use XSLT to transform them into our classes and service classes for consumption by SharePoint.  I will spare you the details of outputting to XML, and just summarise; for each entity, dump out an Entity element, with relevant attributes, then for each CRM attribute, dump out a chile element, again with relevant attributes.  Do the same for entity relationships.  Apply an XSLT to create our data and service classes for each entity.  Finish by combining all of the XML into one large file, and then applying a final XSLT to create the BCS model.

I will go through what is actually generated in a later blog post.  Stay tuned!

About me

I am a software developer in the UK, and specialise in Microsoft technologies.

Starting as a web developer working on the British Airways website at Leighton, I progressed to .net development at Aspire Technology Solutions.  A lot of my work has involved SharePoint, and I have implemented numerous solutions using the platform.  Recently I have been involved in a large-scale enterprise application utilising WCF services, and also a large BI databse using SQL Server technologies.

Please download my CV.

MCTS

Month List