I want to edit the E-mail template in my Dynamics CRM 2011 solution.
I will add a combo box to the form that allows a user to decide what classification the email should be eg. "A", "B" or "C"
This helps our email gateway know what to do with certain mail with regard to archiving etc. Also setting the header will make it harder for a user (recipient) to declassify (lowering a classification) which could easily be done if we just shoved the classification in the subject line (and yes I understand we are still vulnerable to copy and paste but try telling my client that).
Just before the email is sent is there an event where I can get the mail item and add mail headers and also manipulate things like the subject line or some other editable field.
I have written an Outlook Add-in that runs this code on send and basically want to know where I should put similar code in Dynamics.
private Dictionary<string, List<string>> _classifications;
private const string ProtectiveMarkingSchemaName = "http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-Protective-Marking";
private const string Version = "0.1";
private const string Namespace = "xyz.com";
void ApplicationItemSend(object item, ref bool cancel)
{
// GUARD
if (!(item is MailItem)) return;
if (ClassificationDropDown.SelectedItem == null ||
String.IsNullOrEmpty(ClassificationDropDown.SelectedItem.Label))
{
cancel = true;
return;
}
// CURRENT ITEM
var mailItem = (MailItem)Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
// PREPARE MARKING
var origin =
Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
var classification = new StringBuilder();
classification.AppendFormat("SEC={0}", ClassificationDropDown.SelectedItem.Label);
if (DisseminationDropDown.SelectedItem != null)
{
if (!String.IsNullOrEmpty(DisseminationDropDown.SelectedItem.Label))
{
var cat = DisseminationDropDown.SelectedItem.Label;
classification.AppendFormat(", CAVEAT={0}", cat);
}
}
// FILTHY HACK
if (mailItem.Subject == null)
{
mailItem.Subject = " ";
}
// FIND OLD MARKINGS
var start = mailItem.Subject.IndexOf('[');
var end = mailItem.Subject.LastIndexOf(']');
if (end - start > 0)
mailItem.Subject = mailItem.Subject.Remove(start, (end - start) + 1);
// APPLY MARKING
mailItem.Subject = String.Format("{0} [{1}]", mailItem.Subject.TrimEnd(), classification);
mailItem.PropertyAccessor.SetProperty(
ProtectiveMarkingSchemaName,
String.Format("[VER={0}, NS={1}, {2}, ORIGIN={3}]", Version, Namespace, classification, origin));
}
I have a ticket in with Microsoft at the moment but their initial response is that this cannot be done.
Their suggested solution is to use rules on the mail gateway to add the header information which solves that problem if I only want to write the same header info but becomes more complex if I want variable values in the headers.
I had a look a Greg's suggestions and while they were a good learning exercise for me I could not find a solution there.
It is possible to write a workflow that does this but then I would have to account for all the other junk that CRM does when is sends mail and frankly the documentation is a bit sketchy around what I would need to do but my best guess is something like this.
I don't know if this will automatically close the activity or if it retains its context eg (activities in cases, accounts etc) or if this method affects audit so will probably have to account for those things