Tuesday, September 7, 2010

Technical thoughts on the open source Mirosoft Dynamics CRM Framework for EPiServer – Part 3

In part 1 and part 2 I gave an overview of how you can create, read, update and delete CRM entities, and the available methods of doing so. 

We’ve created a CrmContact entity class which we will continue to use in this last part of the blog series. But so far, we’ve only operated on 1:N relationships between entities. This blog post will focus on how you can use the CRM Framework in order to operate on N:N relationships between entities.

N:N Relationships in MS CRM

You can view an entities N:N Relationships from ‘Settings’ –> ‘Customization’ –> ‘Customize Entities’ –> Entity –> ‘N:N Relationships’.

ManyToMany

In the image above, you can see that the Contact entity has a N:N relationship with the Order entity. Now, how do we get all Orders associated with a Contact using this framework?

Creating entity classes

Let’s assume you already have a CrmContact entity class like the one created in part 1. For us to retrieve all Orders assiciated with a Contact entity, we also need to create a CrmOrder entity class:

[CrmEntity("salesorder")]

public class CrmOrder

{

[CrmAttribute("salesorderid")]

public Guid OrderId

{

get; set;

}

 

[CrmAttribute("name")]

public string Name

{

get; set;

}

}

I haven’t included a lot of attributes in this class, as I only want to display the name of the Orders. You should make it a habit of always including the entity’s primary key attribute (in this case OrderId) to your entity classes! This is because you will always need to specify the primary key attribute when doing update operations on an entity. So now we have both the entity classes we need: CrmContact and CrmOrder.

Creating a CrmManyToManyRelationshipController

In the two previous blog posts we’ve used CrmEntityControllers in order to do operations on entities. When working with N:N Relationships we use a different controller: CrmManyToManyRelationshipController<T, V>, where T is the entity we have and V is the entity we want to be returned. In this example we have a Contact entity, and want to retrieve all Orders associated with the Contact, to T is the CrmContact class and V is the CrmOrder class:

CrmControllerFactory factory = CrmControllerFactory.Instance();

CrmManyToManyRelationshipController<CrmContact, CrmOrder> manyRelationshipController =

factory.GetManyToManyRelationshipController<CrmContact, CrmOrder>();

Of course, in order to create a CrmManyToManyRelationshipController, we need a CrmControllerFactory as shown in line 1 of the code above.

Using the Find method

Now that we have our controller, we can use the Find method in order to retrieve all CrmOrders associated with the CrmContact:

CrmManyToManyRelationshipController<T, V>.Find(CrmQuery query) : List<V>

CrmManyToManyRelationshipController<T, V>.Find(CrmQuery query, CrmSortingCriteria sorting) : List<V>

CrmManyToManyRelationshipController<T, V>.Find(CrmQuery query, int page, int pageSize, out int totalCount, CrmSortingCriteria sorting) : List<V>

Example:

Guid contactGuid = new Guid("50F16A2B-904B-DE11-8A4B-0003FFD726F0");

 

CrmQuery query = new CrmQuery();

query.AddCondition(new CrmAttributeCriterion("contactid", contactGuid, CrmConditionOperator.Equal));

List<CrmOrder> orders = manyRelationshipController.Find(query);

What happens in the example above is that we find all Orders of the Contact with contactid == contactGuid. As mentioned in the previos blog post, the CrmQuery class will change at some point. I’m not sure if I’ll change it before the release of the alpha version, but at some point I will definitely improve it!

I still need a couple of testers, so if you’d like to try the framework before it is released, let me know :)

Thursday, September 2, 2010

Would you like to try the open source Microsoft Dynamics CRM framework for EPiServer?

I’ve been working part time on an open source Microsoft Dynamics CRM framework for EPiServer for a couple of months now. And the alpha version is almost ready!

And now it’s time for some testing! I’ve tested everything myself, and I’ve created unit tests for the framework. But still, I want some some more testing to be done before I make the source code public.

So if you’d like to try the framework and help me out at the same time, send me an email: karolikl (at) gmail (dot) com.

Prerequisites: An EPiServer site and Microsoft Dynamics CRM 4.0

After the testing and a code review, the source code will be released on codeplex. And of course, more blog posts are coming up!