When working on a campaigning web application, I had to add a button
to the web page so that when a user clicks on it an event gets added to
their Outlook calender.
There are two ways of doing it:
- Make user download the ics file and save it in the Outlook.
- Directly add the appointment to the Outlook calender.
Using the code
1. Code for downlaoding ics file
First, starting with downloading the ics file, for doing this follow the below steps:
- Create a new appointment in Outlook and save it in iCalender format in your machine.
- Open the saved file in notepad you can see the various parameters which can easily be understood.
- Add the button "Add to Calender" to your web page.
- Add a new handler in the site Handler.ashx and add the below code:
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "text/calendar";
context.Response.AddHeader("Content-Disposition", "inline; filename=\\Calender.ics");
PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN
public bool IsReusable
Add the below code to the button click event:
Now we are done. Run the site click on the button it will give u pop-up to open or save file, Open the file and save & close it. The appointment is in your
outlook calender now.
In the above code you can make the various parameters dynamic or configurable.
2. Code for directly adding to Outlook
Second, directly add the appointment to Outlook. To do this you
have to add a refrence to "Microsoft Outlook Office Library" under COM
and add the namespace for the same above the page:
Add a button "Add to Calender2" to your web page and add the below code in its button click event:
Numprotected void btnCalender2_Click(object sender, EventArgs e)
_Application olApp = (_Application)new Application();
NameSpace mapiNS = olApp.GetNamespace("MAPI");
string profile = "";
mapiNS.Logon(profile, null, null, null);
_AppointmentItem apt = (_AppointmentItem)
apt.Subject = "Test";
apt.Body = "Testing body";
apt.Start = new DateTime(2012, 5, 15, 13, 30, 00);
apt.End = new DateTime(2012, 5, 15, 14, 30, 00);
apt.ReminderMinutesBeforeStart = 15; apt.BusyStatus = OlBusyStatus.olTentative;
apt.AllDayEvent = false;
apt.Location = "";
The above code was taken from: How to Create Birthday Reminders Using Microsoft Outlook, in C#[^], which made my work very easy.
Run the site, click the button, and the appointment must have been added in your Outlook.
Adding an appointment through a web site was not that tough!!!