Track All Staff Emails Anonymously using Dynamics CRM 2011

Paul Nieuwelaar, 08 July 2012

The requirement:
Simply put, we needed to track all emails ‘to’ and ‘from’ any staff member in Dynamics CRM 2011. Not all Staff members are CRM Users, and using the Email Router for each staff member is not an option due to the administrative overhead involved with maintaining each person.

The proposed solution:
Set up a global rule in Exchange to BCC an admin email address on all incoming and outgoing email. That admin email address is then added into CRM as a User, configured with the email router. The idea behind this is that all staff emails, incoming and outgoing will also be sent to the admin mailbox (anonymously), which will then be promoted to CRM using the email router.

The problem:
Not just any email is allowed to be promoted into CRM, before the email is handed from the email router to CRM, there are a few tricky checks that take place. If any of these don’t check out, the email is not added.

The first check is to make sure an email router enabled user is in the ‘To’, ‘CC’, or ‘From’ fields of the email. Because the ‘BCC’ email addresses are stripped from an email, our admin user does not appear to be involved in the email, so the email message is not promoted to CRM.

The next check is to make sure a CRM User is involved in the email message. For example, an email from one Contact to another Contact would not be valid, however an email from a Contact to a User would be valid. Because not all staff members are CRM Users, some emails do not include a User.

The solution:
Luckily, we had a trick or two up our own sleeve. After identifying the Message names used in the 2 checks to CRM from the email router, it was just a matter of intercepting these calls with a plugin of our own, and tricking CRM into thinking the emails should be promoted.

The logic of the plugin was simple; the email router passes CRM information about the ‘To’, ‘CC’, and ‘From’ email addresses when it does its checks, so we simply added on another email address into the BCC field (our admin user’s email address). We registered the plugin on the pre CheckIncoming, and pre DeliverIncoming steps.

The plugin:

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

    string adminEmail = "admin@email.com";
    context.InputParameters["Bcc"] = string.Format("{0};{1}", context.InputParameters["Bcc"], adminEmail);
} 

The registration steps:
CheckIncoming - pre - sync
DeliverIncoming - pre - sync

Note: Both Steps are required as ‘CheckIncoming’ does not modify the actual message, where DeliverIncoming does.

The outcome:
Now when CRM checks that an email router enabled user is involved, it finds our admin in the BCC field, and then when it checks that a user is involved, it also finds our admin. This means every email that hits the inbox of our admin user gets promoted to CRM, and thus all email messages to and from staff members (users or not) get automatically tracked in CRM.