Question

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));
    }
Was it helpful?

Solution 2

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.

  • Use ribbon editor to hide the original send button
  • add a dropdown to the email form with the choices
  • add a new send button with javascript that launches a workflow
  • write a workflow that takes the email looks for the header value field and jams that into the headers
  • then sends it on.

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

OTHER TIPS

This is not an area that I have any direct experience in, but what I can tell you is that the email object that one (usually) works with in the CRM SDK is an abstraction of the underlying email message.

Despite this, there is an area exposed by the SDK which may be of use. Take a look at the section in the SDK called Custom Email Providers for Microsoft Dynamics CRM which looks to expose the something in the direction you want:

An email provider is a pluggable component that is integrated with the Microsoft Dynamics CRM E-mail Router service. The provider is responsible for specialized email processing and interfacing with an email protocol.

That said, I can't see any objects that expose mail headers but I didn't look very hard.

(Note that it redirects to the CRM 4.0 "Email Providers" SDK topic for the useful stuff but this is still valid and supported).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top