Thanks for the detailed answer J, the fact that i am not a coder, made creating the plugin code feel like hitting my head against a wall.
I tried this one:
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 domPlugin : 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 Contact
if (wod_PartyReference.LogicalName == "contact")
{
// Retrieve sender Contact record
Entity wod_Contact = wod_CrmService.Retrieve("contact",
wod_PartyReference.Id, new ColumnSet(true));
// You can write your code for validation, data manipulation here
string piece;
piece = wod_Contact["email"].ToString();
string[] pieces = piece.Split('@');
if (2 == pieces.Length)
{
Entity wod_Account = wod_CrmService.Retrieve("account",
wod_PartyReference.Id, new ColumnSet(true));
string domain;
domain = pieces[1];
//EntityReference Contacts_Domain = new EntityReference();
//Contacts_Domain = pieces[1];
context.OutputParameters["EmailDomain"] = pieces[1];
}
// 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);
}
}
}
}
It is bases on wodEmail plugin. but i am not able to get the value.
Any help would be appreciated. I am posting his in public cause i think many come across the same issue.
Regards,
A.