Click here to Skip to main content
15,867,190 members
Articles / Web Development / ASP.NET
Tip/Trick

"Add to Calender": Adding event to Microsoft Outlook through Web Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
18 May 2012CPOL2 min read 60.7K   1.6K   22   8
Adding event to Microsoft Outlook through Web Application

Introduction

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:

  1. Make user download the ics file and save it in the Outlook.
  2. 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:

  1. Create a new appointment in Outlook and save it in iCalender format in your machine.
  2. Open the saved file in notepad you can see the various parameters which can easily be understood.
  3. Add the button "Add to Calender" to your web page.
  4. Add a new handler in the site Handler.ashx and add the below code:
C++
public void ProcessRequest(HttpContext context)
{
    context.Response.Clear();
    context.Response.ContentType = "text/calendar";
    context.Response.AddHeader("Content-Disposition", "inline; filename=\\Calender.ics");
    context.Response.BinaryWrite(new System.Text.ASCIIEncoding().GetBytes(@"BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN
VERSION:1.0
BEGIN:VEVENT
DTSTART:20120515T110000Z
DTEND:20120515T113000Z
LOCATION:office
UID:040000008200E00074C5B7101A82E00800000000E09778CC8A2FCD01000000000000000
	010000000A616AC829938554DA102762D344287C7
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:=
=0D=0A
SUMMARY:calander
PRIORITY:3
END:VEVENT
END:VCALENDAR"));
        //Response.Write(); 
        context.Response.End();
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

Add the below code to the button click event:

C#
Response.Redirect("Handler.ashx");

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:

C#
using Microsoft.Office.Interop.Outlook;

Add a button "Add to Calender2" to your web page and add the below code in its button click event:

C#
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)
            olApp.CreateItem(OlItemType.olAppointmentItem);

    // set some properties
    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;           // Number of minutes before the event for the remider
    apt.BusyStatus = OlBusyStatus.olTentative;    // Makes it appear bold in the calendar

    apt.AllDayEvent = false;
    apt.Location = "";

    apt.Save();
}

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.  

Conclusion  

Adding an appointment through a web site was not that tough!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoes not work on server. Pin
Member 1111760917-Jan-17 20:11
Member 1111760917-Jan-17 20:11 
QuestionI think this is not entirely true Pin
Member 283852815-May-13 1:25
Member 283852815-May-13 1:25 
QuestionCOM error Pin
navadeebans12-Sep-12 23:53
navadeebans12-Sep-12 23:53 
GeneralAdding Private Appointment to outlook calender Pin
Nilesh (Puna)9-Jul-12 0:57
Nilesh (Puna)9-Jul-12 0:57 
how to Add private appointment to outlook calender using asp .net .

-- modified 31-Jul-12 7:11am.
GeneralMy vote of 5 Pin
Nilesh (Puna)15-May-12 20:37
Nilesh (Puna)15-May-12 20:37 
GeneralRe: My vote of 5 Pin
deeptibansal15-May-12 22:44
deeptibansal15-May-12 22:44 
GeneralMy vote of 5 Pin
maneesh@path15-May-12 0:11
maneesh@path15-May-12 0:11 
GeneralRe: My vote of 5 Pin
deeptibansal15-May-12 17:54
deeptibansal15-May-12 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.