I recently had an interesting situation with one of our clients who wanted to use a custom entity modeled on the Accounts entity to view their customer data in CRM 2011 and CRM online. The difference was that this entity would have a N:N Relationship with Contacts instead of a 1:N relationship; however, they still wanted to be able to view a rollup of all the Activities and Opportunities from the related Contact and Opportunity records on the custom entity form (see the class diagram below)
Now, CRM makes it really easy to view that for Accounts using the Associated Views but the same cannot be said for custom entities. There were a couple of different options coming to mind that I knew would achieve the effect– for instance, creating a SSRS report to pull out all the Activities and Opportunities associated with the related Contact and Opportunity records for a particular custom entity record. Another option would include using JavaScript with Silverlight. The catch though, was that the client wanted to have all the records appear in a standard grid on the entity form.
So, I did some research and found an article that explains a plugin to handle the RetrieveMultiple message. However, this article was written for CRM 4.0, which is significantly different from our target CRM version CRM 2011 or CRM online. To make it work for the new version, I rewrote the code and utilized the streamline development toolkit provided in CRM SDK 5. The experiment proved successful and I thought it would be helpful to share the solution with other CRM developers out there. Here’s a step by step breakdown of what I did:
Step 1. Create Dynamics CRM 2011 Package project and connect to CRM.
Step 2. In CRM Explorer, right click on the custom entity and select Create plug-in.
Step 3. In the Create Plug-in dialog, select values as below. CRM SDK tool kit will automatically generate a CRM plugin project and the structural code. You just need to implement the PreActivityRetrieveMultiple function as describe in the next step.
Step 4. Implement PreActivityRetrieveMultiple function as below. Some code has been omitted.
[sourcecode language=”csharp”]
protected void ExecutePreActivityRetrieveMultiple(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException(“localContext”);
}
//Get the plugin Execution Context and Organization Service
…
//find all child entities based on the unsecure configuration XML
foreach (XmlNode child in childentities)
{
string childentityname = child.SelectSingleNode(“./ChildEntityName”).InnerText;
string lookupfield = child.SelectSingleNode(“./Lookup”).InnerText;
string lookupidfield = child.SelectSingleNode(“./Lookupid”).InnerText;
QueryExpression query = new QueryExpression()
{
…
};
try
{
EntityCollection ec = service.RetrieveMultiple(query);
foreach (Entity childentity in ec.Entities)
childids.Add((childentity[lookupidfield]));
}
catch (SoapException e)
{
throw new Exception(e.Detail.InnerText);
}
}
//add all child entities into the link criteria for the execution context
if (childids.Count == 0) return;
condition.Operator = ConditionOperator.In;
condition.Values.AddRange(childids);
}
}
[/sourcecode]
Step 5. Build and deploy the project. With CRM developer toolkit, you can deploy CRM plugin or custom workflow activity within Visual studio.
Step 6. Test the plugin. Verify the activity associated view in the custom entity. In the standard activities associated view of the custom entity, you should be able to see the activities from child contacts and child opportunities.
As some might have already noted by now, one of the downsides of working with the CRM SDK 5 is that it’s quite different from previous version and some components in the previous version are no longer supported or changed, which can often frustrate the developer. However, I feel that it does streamline the development process of CRM plugins and custom workflow activities and can help to improve the development productivity. Developers can also make other CRM development and customization within Visual Studio.