Programmatically creating Outlook appointments

Outlook Reference

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.
CreateItem(Microsoft.Office.Interop.
Outlook.OlItemType.olAppointmentItem);

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);

Reminder settings

appointmentItem.ReminderSet = true;
appointmentItem.ReminderMinutesBeforeStart = 60;

Subject, location, and saving

appointmentItem.Subject = "Be Somewhere";
appointmentItem.Location = "Location 1";
appointmentItem.Body = "blah blah blah";
appointmentItem.Save();

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 = "sysadmin@corp.net"
mailItem.To = "me@me.com";
mailItem.Send();
}

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.

Advertisements