CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

HowTo: Create an Outlook Appointment Item in ASP .NET

Here's some neat code I put together for my company's Asset Management product.  Basically users can assign scheduled maintenance to their assets (example: on dec 1st, 2004 defrag the hard drive of this pc).  I wanted to give them an easy way of reminding themselves to do the maintenance without having to log into the program and check their alerts.  I wanted this to be specific to each asset since no one person is likely to be responsible for everything.

Doing some research, I found that the outlook calendar appointment is just a formatted text file.  So I decided to generate the format, slap a .vcs extension on it, and then stream it to the user.  In addition, I put it into a popup window (with querystring to get the specific record, that's irrelevant though so I stripped that code out) and did all of the generation and stream in the page load.  By ending the response in page load, the user will be presented with a dialogue download box and never actually see the popup window, so it appears to them as if they just clicked a “file://“ link.

The only thing I don't like about this technique so far is that it has to save the file to disk before streaming.  Ideally I'd like to stream from memory and be done with it.  Barring that I'll just set up a script to run on the server every night to blow out the temporary download directory.

Anyway, here's the code, as always, comments are appreciated:

private void Page_Load(object sender, System.EventArgs e)

{

  string fileName = @"C:\YourDownloadPath\YourFile.vcs";

  System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName);

  writer.WriteLine("BEGIN:VCALENDAR");

  writer.WriteLine(@"PRODID:-//Microsoft Corporation//Outlook MIMEDIR//EN");

  writer.WriteLine("VERSION:1.0");

  writer.WriteLine("BEGIN:VEVENT");

  DateTime dueDate;

  dueDate = DateTime.Parse(maintenance.DueDate);

  //Start and End Date in YYYYMMDDTHHMMSSZ format

  //I set all times to T170000Z (noon eastern time) since I only care about date

  //The user can set the time when they open the appointment item.

  writer.WriteLine("DTSTART:" + dueDate.Year.ToString() + dueDate.Month.ToString() + dueDate.Day.ToString() + "T170000Z");

  writer.WriteLine("DTEND:" + dueDate.Year.ToString() + dueDate.Month.ToString() + dueDate.Day.ToString() + "T170000Z");

  //Lets set the appointment location to the actual location of the asset.

  writer.WriteLine("LOCATION:" + asset.Site.SiteDescription + " - " + asset.Location.LocationDescription + " - " + asset.Room.RoomDescription);

  writer.WriteLine("CATEGORIES:Maintenance");

  //Toss the description of maintenance into the notes field.

  writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + maintenance.MaintenanceDescription + "=0D=0A");

  //For summary, let's tell them what kind of asset it is

  writer.WriteLine("SUMMARY:" + asset.Product.ShortDescription + " Maintenance");

  writer.WriteLine("PRIORITY:3");

  writer.WriteLine("END:VEVENT");

  writer.WriteLine("END:VCALENDAR");

  writer.Close();

  //File is generated, now let's stream it out via attachment download

  System.IO.FileInfo file = new System.IO.FileInfo(fileName);

  Response.Clear(); // clear the current output content from the buffer

  Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);

  Response.AppendHeader("Content-Length", file.Length.ToString());

  Response.ContentType = "application/octet-stream";

  Response.WriteFile(file.FullName);

  Response.End();

}



Comments

Eric Wise said:

Awesome, I'll try that out tonight!
# August 26, 2004 9:38 AM

Eric Wise said:

Well I did get the memorystream working which is an improvement. I did have to call streamwriter.flush after every writeline though. Otherwise the memorystream never got any data. Very odd.
# August 26, 2004 1:13 PM

Eric Wise said:

Ah Ha! streamWriter.AutoFlush = true;
# August 26, 2004 1:16 PM

Eric Wise said:

HowTo: Create an outlook appointment in ASP .NET Part II
# August 26, 2004 3:21 PM

DarthPedro said:

I don't even program much in ASP.NET, but I thought this was very cool...
# August 27, 2004 5:56 AM

rohit said:

What about retrieving contacts from Outlook and Outlook Express. Is there any way I could retrieve it from an ASP.NET application?
# October 10, 2004 8:28 PM

Eric Wise said:

I can't say I've ever tried to do it the other way. A few things jump to mind when thinking of such a request though.

1) I would think that outlook has some form of contact export which probably puts them in a flat file or xml. Said file could be uploaded and parsed.

2) I doubt that you would want to try to just read such a thing without a manual upload. Having a webpage that automatically grabs something from my hard drive / environment would make me very nervous from a security standpoint.
# October 11, 2004 6:49 AM

sunil said:

How to import contacts from out look express in ASP.net. I have written a code in VB.net , but it is failing in webform
# November 10, 2004 7:26 PM

Priya said:

When I try to run this code, I get an error like "Cannot import vCalendar. One or more parameter values are invalid".
Please help.
# July 5, 2005 9:33 AM

Eric Wise said:

What version of outlook are you on? I found that this code only works for 2003. If you use a different version then save a calendar item on your desktop, pop it open and notepad, and change the code to reflect the format.
# July 9, 2005 6:38 AM

Jannik Nilsson said:

Do you know how to access a Calendar, User directory, appointment and so on from an Exchange Server?
# July 14, 2005 1:13 AM

Ben said:

Is there a comparable way to create an Outlook contact also?
# September 23, 2005 2:12 PM

Ana said:

i've tryied to write this code in C# but it doesn't recognize "asset" or "maintenance"..it can work in C#?if it can..which packeges I have to use?
Thank you
# March 23, 2006 6:07 AM

Kyle Hancock said:

Hello Eric,

I realize this is an old post, but I am able to get your code to work, but at this point all I have done is save the .vcs file and import it into Outlook.  Is there a way to send this via e-mail directly to the user so they receive simply a meeting request?

# December 5, 2006 9:50 AM

ph said:

how to modify the appointment ?

# December 3, 2007 2:12 AM

phani said:

Ho wto modify the previously created appointment ?

# December 3, 2007 2:13 AM

Eric said:

Can you include more then 1 email in the required field? if so HOW? this does nothing:

Writer.WriteLine("ATTENDEE;CUTYPE:INDIVIDUAL;ROLE=OPT-PARTICIPANT;RSVP=TRUE:MAILTO:" & moreemails)

in ASP only please.

# March 17, 2008 3:19 PM

dvd decrypter software said:

As reported before, iTunes has a problem with Vista. The new iTunes version 7. 1. 1., released last week, which packs a lot of new features, fixes some of these problems, but not all. As I report in my other blog (iTimes), iTunes has been updated to accommodate

# June 27, 2008 7:06 AM

free dvd burning software said:

Fall back to GetProcAddress entry in another DLL version.

# July 1, 2008 5:23 PM

shrink dvd said:

Mondorescue backs up your GNU/ Linux server or workstation to tape, CD- R, CD- RW, DVD- R[ W], DVD R[ W], NFS or hard disk partition. In the event of catastrophic data loss, you will be able to restore all of your data[ or as much as you want], from bare

# July 3, 2008 10:06 AM

dvd decoders for xp said:

Additionally, even in scenarios wher Cable/ DSL is provided by your Internet Service Provider, there can be connection/ configuration issues that slow down your network connection to a crawl. Please run one of the Bandwidth Speed Tests to see how fast

# July 8, 2008 6:06 AM

how copy dvd said:

Assistance from RWC Computer Services Your browser doesn\'t support IFrames. a href \"http:// siteneighbors. com/ blogs/ widget/ 86\" target \"_ blank\" Visit this blog\'s neighborhood. /

# July 24, 2008 4:06 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications