Kanini Kanini - 2 years ago 77
C# Question

CRM 2011 Removing Activity Party from the Email CC List

How can I remove Activity Party from an Email CC field in CRM 2011 via a Plugin?

I have done so far, but I am stuck beyond this. My problem is that the Email Router is duplicating contacts with the same email address in the CC field. We have suggested that de-duplicating the contacts is the best solution, however that solution has not been accepted. So, for now, we need to be able remove activity parties from the email CC field and replace it with an unresolved email address.

Any thoughts?

Email email = crmService.Retrieve(Email.EntityLogicalName, emailId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)).ToEntity<Email>(); //Do not use ColumnSet(true)
List<ActivityParty> ccPartyList = email.Cc.ToList<ActivityParty>();
List<string> ccEmailAddresses = new List<string>();

foreach (ActivityParty ap in ccPartyList)

List<string> dedupedCCEmailAddresses = ccEmailAddresses.Distinct().ToList();
List<string> emailAddressToBeRemoved = new List<string>();

//Check for each unique email addresses, how many records are there
foreach (string emailAddress in dedupedCCEmailAddresses)
int count = ccPartyList.Count(ap => ap.AddressUsed.Equals(emailAddress, StringComparison.InvariantCultureIgnoreCase));
if (count > 1) //Same email address; Multiple Records

//Remove ALL Activity Party from the List
foreach (string emailAddress in emailAddressToBeRemoved)
ccPartyList.RemoveAll(ap => ap.AddressUsed.Equals(emailAddress, StringComparison.InvariantCultureIgnoreCase));
ActivityParty unResolvedEmailAddress = new ActivityParty();
unResolvedEmailAddress.AddressUsed = emailAddress;

Email emailToUpdate = new Email();
emailToUpdate.Id = emailId;
emailToUpdate.Cc = ccPartyList;

I have now updated it with the following code, wherein I remove all CC parties.

if (localContext == null)
throw new ArgumentNullException("localContext");

IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
tracingService = localContext.TracingService;

Email emailFromTarget = (context.InputParameters.Contains("Target")
&& context.InputParameters["Target"] is Entity)
((Entity)context.InputParameters["Target"]).ToEntity<Email>() : null;

Entity email = service.Retrieve(Email.EntityLogicalName, emailFromTarget.Id, new ColumnSet("cc"));

EntityCollection cc = email.GetAttributeValue<EntityCollection>("cc");
if (cc != null)
cc.Entities.ToList().ForEach(party =>

email["cc"] = cc;

Even, then the CC field continues to hold on to the data post creation.

I am doing it in the Post Create Plugin. Is that way is it not removing them? Do I have to do it as a Workflow / CWA?

Answer Source

Instead of post-operation, register it in pre-operation of Create plugin & remove the dupe CC partylist from target entity. This will work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download