Found myself needing to learn the basic of using the Microsoft Office API to programmatically creating appointments in Outlook. The event will be initiated from SharePoint 2010, for various reasons I was unable to use the standard Outlook – SharePoint integration.
In this example I’ll demonstrate the basics of creating an Outlook Appointment…
First step is to add a reference into Visual Studio, Reference: Microsoft.Office.Interop.Outlook.
Once the reference is added you need to create a new Outlook Application, and then instantiate the object type you require, in this example…an Outlook appointment item.
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem =(Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.
As you’ve added the reference you get all the goodness of the intelliSense, obviously makes everything a lot easier.
Specify the Start and End date / time
appointmentItem.Start = DateTime.Now;
appointmentItem.End = DateTime.Now.AddHours(1);
appointmentItem.ReminderSet = true;
appointmentItem.ReminderMinutesBeforeStart = 60;
Subject, location, and saving
appointmentItem.Subject = "Be Somewhere";
appointmentItem.Location = "Location 1";
appointmentItem.Body = "blah blah blah";
Instead of calling ‘Save’ the appointment can be forwarded as a VCal attachment
protected void sendAsVcal(Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem)
Microsoft.Office.Interop.Outlook.MailItem mailItem = appointmentItem.ForwardAsVcal();
mailItem.SenderEmailAddress = "email@example.com"
mailItem.To = "firstname.lastname@example.org";
I initially built this as a console app to test the functionality, but can easily be moved to a standard web application project. Code above tested on Outlook 2007 / 2010.