chipshotx
Full Access Member
anyone?
Add a Microsoft Outlook Appointment from ASP.NET
Add appointments to your users' Outlook calendars from your Web site.
By Ken Getz, technical editor
Q: Recently, I visited a Microsoft site to sign up for an upcoming event. When I was done, the site offered me the option to create an Outlook appointment for the item. When I clicked the button, the site created a new appointment item on my computer, which I saved in Outlook. How did they do that? How could they communicate with my copy of Outlook, for their server? I'd like to do this on my own sites! -- Melony
A: Funny, my co-author Paul Sheriff recently showed me some code that did something similar (it helps to have smart friends, obviously). When you visit a site that creates a calendar item for you, the site simply sends a standard vcalendar item in the response stream -- it's up to you to save the item in Outlook.
I modified Paul's code to do exactly what you're requesting -- it's not all that hard, once you know the tricks. The important part is getting the basic concept: If you want to generate an Outlook appointment from a Web page, your job is to write information into the Response stream, and that information must include headers that indicate to the browser that you're sending an appointment.
You might add a button to a page, and add this code to the button's Click event handler:
Dim icalendar As String = BuildApptItem()
Response.ContentType = "text/x-vCalendar"
Response.AppendHeader("content-disposition", _
"inline; filename=foo.vcs")
Response.Write(icalendar)
Response.End()
Setting the ContentType property and appending information into the header are crucial steps for this process.
You'll also have to create the content of the appointment item, which is all in the BuildApptItem method:
Private Function BuildApptItem() As String
' Obviously, these values would come from the UI somewhere...
Return BuildVCalendar(#12/5/2005#, _
#12/5/2005 2:00:00 AM#, "Visit with Mom", _
"Yearly visit", 3)
End Function
The BuildApptItem method calls another method, BuildVCalendar, passing in start and end dates, a description, a summary/title, and a priority value. Normally, you'd get these values from the user interface; in this example, I've hard-coded them. The BuildVCalendar procedure actually builds up the necessary information for the vcalendar item:
Private Function BuildVCalendar(ByVal startDate As DateTime, _
ByVal endDate As DateTime, ByVal description As String, _
ByVal summary As String, ByVal priority As Integer) As String
Dim sw As New System.IO.StringWriter
sw.WriteLine("BEGIN: VCALENDAR")
sw.WriteLine("VERSION:1.0")
sw.WriteLine("BEGIN: VEVENT")
'EXAMPLE: sw.WriteLine("DTSTART:20050314T160000Z ")
sw.WriteLine("DTSTART:{0:yyyyMMdd\THHmmss\Z}", _
startDate.ToUniversalTime)
sw.WriteLine("DTEND:{0:yyyyMMdd\THHmmss\Z}", _
endDate.ToUniversalTime)
sw.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{0}", _
description)
sw.WriteLine("SUMMARY;ENCODING=QUOTED-PRINTABLE:{0}", summary)
sw.WriteLine("PRIORITY:{0}", priority)
sw.WriteLine("END:VEVENT")
sw.WriteLine("END:VCALENDAR")
Return sw.ToString()
End Function
There are many more items that make up the body of the vcalendar item. For more information, you can start with Microsoft's KB article 287625 (http://support.microsoft.com/default.aspx?scid=kb;en-us;287625). For demo purposes, however, the fields included here do the trick.
To try out the page, simply run the project and click on the button. You'll be prompted to open or save the calendar item, and if you select Open, you'll see a new calendar item. (If you click on Save, you'll save the VCS file to disk, and will still have to open it and save it in Outlook to have the item appear in your calendar.)
Obviously, there's a lot more to this technique, but Paul's code got me started creating a simple solution to your question.
-- Ken