2
votes

I create a workflow that sends notification email, which is triggered by "Appointment created" event.

I want it to send meeting invitation (which could be accepted/rejected in Outlook), not the regular email, what is the trick here?

1

1 Answers

1
votes

Create a mail message as a custom workflow activity and send it via SMTP :

  • Develop a custom workflow activity with appropriate input parameters (start time, end time, smtp server name etc.) which you can configure to map to CRM entity fields.
  • In your C# code, you will need to generate a iCal event. You could do this by reading and understanding the iCal spec and generating a string manually in the correct format like this, or just use a library like the the DDay.iCal library. Note, if you use this library, you will either need to deploy this dll in the GAC, or merge it using ILMerge when you build the workflow activity.
  • Convert the iCal object back to a string.
  • Send the iCal as an email via smtp (note, smtp likely won't work from CRM online sandbox, only on premise) but change the content type of the MailMessage to "text/calendar"
  • Message should arrive as an acceptable calendar in Outlook which will display based on how you have configured the iCal event properties.
  • Configure the workflow activity against the appropriate entity (Appointment / Service activity).

Sample approach in code below - note, not production ready. You need to understand the iCal properties you want to set and slot this into the boilerplate workflow activity code accordingly.

    // 3rd party libraries to reference
    using DDay.iCal;
    using DDay.iCal.Serialization.iCalendar;
    ...
    ...
    // Create the iCal
    IICalendar iCal = new iCalendar();
    iCal.Method = "Request";
    ...
    ...
    IEvent evt = iCal.Create<Event>();
    evt.Summary = summary;
    evt.Start = new iCalDateTime(eventstartDt).SetTimeZone(local);
    evt.End = new iCalDateTime(eventendDt).SetTimeZone(local);
    var serializer = new iCalendarSerializer(iCal);
    var iCalString = serializer.SerializeToString(iCal);
    ...
    ...
    var mailMessage = new MailMessage
    {
       Subject = Summary.Get(_executionContext),
       From = new MailAddress(FromEmailAddress.Get(_executionContext))
    };

    // Create the Alternate view object with Calendar MIME type
    var ct = new System.Net.Mime.ContentType("text/calendar");
    if (ct.Parameters != null) ct.Parameters.Add("method", "REQUEST");

    //Provide the framed string here
    AlternateView avCal = AlternateView.CreateAlternateViewFromString(iCalString, ct);
    mailMessage.AlternateViews.Add(avCal);

    // Send email
    try
    {
       smtpClient.Send(mailMessage);

    }
    catch (Exception ex)
    {
       // Log it.
    }