Second Life of a Hungarian SharePoint Geek

October 9, 2012

Accessing meeting request properties from mail event receivers

Filed under: Calendar, Incoming email, SP 2010 — Tags: , , — Peter Holpar @ 23:34

Assume you have an incoming mail enabled SharePoint calendar, you send meeting requests to the calendar and need to access their properties (like the start or end time of the meeting)  from the code of your SPEmailEventReceiver.

The incoming meeting requests contain the information about the meeting as a separate attachment in the iCalendar format.

You can read about the iCalendar standard on Wikipedia, like

“By default, iCalendar uses the UTF-8 character set.”

moreover

“iCalendar data has the MIME content type text/calendar.”

Although for parsing this content you can’t find the necessary methods in the core SharePoint object model, SharePoint uses the CalendarReader (and related) objects located in the Microsoft.Internal.Mime assembly from GAC when processing incoming meeting requests.

The following code illustrates, how to get a property by its name (see the Wikipedia article for possible values) from the SPEmailMessage object received in your event handler.

  1. private string GetCalendarProp(SPEmailMessage mail, string propName)
  2.         {
  3.             string propValue = null;
  4.  
  5.             foreach (SPEmailAttachment att in mail.Attachments)
  6.             {
  7.                 if (att.ContentType == "text/calendar")
  8.                 {
  9.                     using (Stream stream = att.ContentStream)
  10.                     {
  11.                         CalendarReader cr = new CalendarReader(stream, "utf-8", CalendarComplianceMode.Loose);
  12.                         while (cr.ReadNextComponent() && string.IsNullOrEmpty(propValue))
  13.                         {
  14.                             if (cr.ComponentId == ComponentId.VEvent)
  15.                             {
  16.                                 CalendarPropertyReader pr = cr.PropertyReader;
  17.  
  18.                                 while (pr.ReadNextProperty() && (string.IsNullOrEmpty(propValue)))
  19.                                 {
  20.                                     if (pr.Name == propName)
  21.                                     {
  22.                                         propValue = pr.ReadValueAsString();
  23.                                     }
  24.                                 }
  25.                             }
  26.                         }
  27.                     }
  28.                     break;
  29.                 }
  30.             }
  31.  
  32.             return propValue;
  33.         }

Because it is rather cumbersome to test event receivers, you can temporarily stop the SPTimer service (don’t forget to restart it later!), send your meeting request to SharePoint, and when the mail arrives to the Drop folder of SMTP, copy a backup to another folder. Next, you can use a console application with a method like this one below to read the SPEmailMessage directly from the file:

  1. private void SPEmailMessageTest()
  2. {
  3.     using (FileStream fs = new FileStream(@"C:\temp\appointment – 159d46ff01cda09100000003.eml", FileMode.Open))
  4.     {
  5.         SPEmailMessage mail = new SPEmailMessage(fs, "test");
  6.         string uid = GetCalendarProp(mail, "UID");
  7.     }
  8.  
  9. }

The sample method above reads the UID property of the meeting that is an internal ID to look up and update former corresponding calendar items.

Advertisements

2 Comments »

  1. How did you get access to the CalendarReader object in your code above? Trying to parse SP emails as well.. Thanks!

    Comment by JR — March 15, 2013 @ 15:01

    • The CalendarReader class is located in the Microsoft.Internal.Mime assembly (GAC). I assume that was installed by SharePoint.

      Comment by Peter Holpar — May 11, 2013 @ 22:43


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: