Monday, August 9, 2010

Technical thoughts on the open source Microsoft Dynamics CRM Framework for EPiServer – Part 1

Last week I announced that I’ve made up my mind about creating an open source MS CRM framework for EPiServer. The development has been going on since then, and in this blog post I’ll give some insight to what the framework will be like. Please keep in mind that the framework is in its alpha phase, and based on feedback, future code reviews and further development, the code might change! So consider this a draft :)

MS CRM entities and attributes

Before starting, it’s important to know the difference between entities and attributes. An entity such as Contact contains attributes such as Firstname and Lastname. Entities can be related to each other through one-to-many and many-to-many relationships. 

You can view all entities and attributes in ‘Settings’ –> ‘Customization’ –> ‘Customize Entities’ in CRM:

crm entities

You can view an entities attributes from ‘Customize Entities’ –> Entity –> ‘Attributes’:

crm attributes

Web.config

Integration with EPiServer is of course the main focus of this framework, so I’ve put all connection specific details in a separate configSection in the web.config file.

In <configSections>:

<sectionGroup name="crmFramework">

   <section name="crmConnector" type="Custom.CrmConnector.Configuration.CrmConnectorSection" />

</sectionGroup>

The configuration section itself:

<crmFramework>

   <crmConnector crmUrl="http://customer.crm.com:5555" username="UserName" password="Password" domain="Domain" organization="CrmOrg" />

</crmFramework>

Creating an Entity class in EPiServer

If you want to get an entity from CRM with all its attributes, this results in a lot of data being transferred and hence the operation can get time consuming. So in order for you to chose exactly what attributes you need, you need to create a class for each entity specifying its attributes. You can do CRUD operations on all CRM entities, standard or custom, and the framework supports all CRM attribute types.

The class needs a CrmEntity attribute speficying the CRM entity name. Each CRM attribute is defined as a public property with a CrmAttribute attribute specifying the attributes CRM name.

Here’s an example of a class created for the CRM Contact entity:

[CrmEntity("contact")]

public class CrmContact

{

// Has to be included for update operations

   [CrmAttribute("contactid")]

   public Guid ContactId

   {

      get;

      set;

   }

 

   [CrmAttribute("firstname")]

   public string FirstName

   {

      get;

      set;

   }

 

   [CrmAttribute("lastname")]

   public string LastName

   {

      get;

      set;

   }

}

Creating a CrmControllerFactory and CrmEntityControllers

In order to use the entity class we just created, we’ll need to get an instance of the CrmControllerFactory class. This is a singleton class handling authentication towards CRM and the CrmEntityControllers.

A CrmEntityController is a generic controller for doing operations on CRM entities. Through the controller you can access methods like Create, Delete, Find, Get, Update etc.

So let’s create a CrmControllerFactory and a CrmEntityController for the CrmContact class created above:

CrmControllerFactory factory = CrmControllerFactory.Instance();

CrmEntityController<CrmContact> crmContactController = factory.GetEntityController<CrmContact>();

As mentioned before, we can now use the crmContactController to do operations on CRM Contact entities. An example is getting a contact by Guid:

CrmContact contact = crmContactController.Get(new Guid("309B8DB0-C5DF-DC11-B07C-0003FF360638"));

string firstName = contact.FirstName;

All the methods available through the CrmEntityController<T> class will be covered in Part 2 of this blog series (in a couple of days). In the future, after a code review, I will also post some code showing the framework itself.

And remember, things might change! If you see anything you already disagree with, please let me know, I’m up for a discussion :)

No comments:

Post a Comment