Save an Outlook appointment from ASP.NET 

Outlook supports import/export operation via a file format called vCalendar. vCalendar files are standardized text files that contain all the information held inside an appointment. What’s important for us (as asp.net developers) is that outlook will open vCalendar files (.vcs) when signaled to do so from a web browser. But first things first, here’s the format for the vCalendar file.

BEGIN:vCalendar
VERSION:5.0
PRODID:-//Microsoft Corporation//Works 2000//EN
BEGIN:vEvent
DTSTART:20011225T000000
DTEND:20011226T000000
SUMMARY;ENCODING=QUOTED-PRINTABLE:Christmas
CATEGORIES;ENCODING=QUOTED-PRINTABLE:Gifts, Holiday
PRIORITY: 3
END:vEvent
END:vCalendar

You can also look at the .vcs format by exporting any outlook appointment by choosing file->save as and selecting the .vcs format. Now for the important part. Below is an example of how to write a vCalendar file from your asp.net application.

string uid = Guid.NewGuid().ToString().Replace("-", ""); 
string start = DateTime.Now.AddHours(1).ToString("yyyyMMddTHH0000"); 
string end = DateTime.Now.AddHours(2).ToString("yyyyMMddTHH0000"); 
this.Response.Clear(); 
this.Response.Charset = ""; 
this.Response.ContentType = "text/x-vCalendar"; 
this.Response.AddHeader("Content-Disposition", "filename=Event.vcs;"); 
string mask = "BEGIN:VCALENDAR\n"
    + "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\n"
    + "VERSION:1.0\n"
    + "BEGIN:VEVENT\n"
    + "DTSTART:{0}\n"
    + "DTEND:{1}\n"
    + "UID:{5}\n"
    + "SUMMARY;ENCODING=QUOTED-PRINTABLE:{2}\n"
    + "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{3}\n"
    + "LOCATION;ENCODING=QUOTED-PRINTABLE:{4}\n"
    + "PRIORITY:3\n"
    + "End:VEVENT\n"
    + "End:VCALENDAR";
this.Response.Write(string.Format(mask, start, end, subject, description, location, uid));
this.Response.End();

First, we need to clear the response stream of any output that has already been buffered. Then set the ContentType to the MIME header associated with the vCalendar file (text/x-vCalendar). Setting the content-disposition header is also important because this is the filename that the user will see. Now we can begin to write the output. I have choosen to create a mask and fill it in with the various data elements for our appointment. The final step is to write reponse output and end the response. When this code executes, the user will see the standard “open or save” dialog inside internet explorer. If the user chooses to open the file, outlook will be launched and the appointment detail screen will be displayed with the information we have specified in the code. spiffy!

Comments
No Comments Available
Add a New Comment
Name

Email Address

Url

Comment