Sometime there is a requirement to execute custom code for performing custom validations, extract data or manipulate data on receiving incoming email in Microsoft Dynamics CRM 2011. The following step by step guide steps explains to develop and register a CRM SDK plugin to achieve this functionality.
[Instructions]1. Use the plugin code below or download the plugin solution wod.Crm.IncommingEmail, make required changes and build plugin assembly file.
2. Use plugin registration tool to register the plugin assembly and then register a new plugin step for Email entity on Create event with Pre-Operation event pipeline stage in Synchronous execution mode
[Plugin: C# Code]
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using Microsoft.Xrm.Sdk; |
using Microsoft.Xrm.Sdk.Metadata; |
using Microsoft.Xrm.Sdk.Query; |
using Microsoft.Crm.Sdk; |
namespace wod.Crm.IncommingEmail |
{ |
public class wodPlugin : IPlugin |
{ |
public void Execute(IServiceProvider serviceProvider) |
{ |
// Obtain the execution context from the service provider. |
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) |
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); |
IOrganizationServiceFactory wod_serviceFactory = null; |
IOrganizationService wod_CrmService = null; |
try |
{ |
wod_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService( typeof(IOrganizationServiceFactory)); |
wod_CrmService = wod_serviceFactory.CreateOrganizationService(context.UserId); |
if (context.MessageName == "Create" && |
context.InputParameters.Contains("Target") && |
context.InputParameters["Target"] is Entity |
) |
{ |
Entity wod_PluginEntity = (Entity)context.InputParameters["Target"]; |
//Checking if plugin is trigger for Email entity |
if (wod_PluginEntity.LogicalName == "email") |
{ |
// Checking if email direction is incomming |
if (wod_PluginEntity.Contains("directioncode")) |
{ |
// Checking if email is incomming |
if (((Boolean)wod_PluginEntity["directioncode"]) == false) |
{ |
EntityCollection wod_IncommingParty = null; |
wod_IncommingParty = (EntityCollection)wod_PluginEntity["from"]; |
// Checking if plugin entity From field is activityparty entity object |
if (wod_IncommingParty != null && wod_IncommingParty[0].LogicalName == "activityparty") |
{ |
EntityReference wod_PartyReference = (EntityReference)wod_IncommingParty[0]["partyid"]; |
// Checking if email is sent by CRM Account |
if (wod_PartyReference.LogicalName == "account") |
{ |
// Retrieve sender Account record |
Entity wod_Account = wod_CrmService.Retrieve("account", wod_PartyReference.Id, new ColumnSet(true)); |
// You can write your code for validation, data manipulation here |
throw new Exception("email account: " + wod_Account["name"]); |
} |
} |
} |
} |
} |
} |
} |
catch (System.Web.Services.Protocols.SoapException ex) |
{ |
throw new InvalidPluginExecutionException(ex.Detail.InnerText); |
} |
catch (Exception ex) |
{ |
throw new InvalidPluginExecutionException(ex.Message); |
} |
} |
} |
} |