Eric Wise

Sponsors

The Lounge

Blogs I Read

Fun & Games

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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();

}


Posted 08-26-2004 1:13 PM by Eric Wise

[Advertisement]

Comments

Bogo wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 08-26-2004 9:34 AM
StreamWriter has a contstructor that takes any underlying System.IO.Stream. Create a new MemoryStream and pass it in. Then just Response.Write() the contents of the MemoryStream (you will however, have to convert that to a string or a character array using System.Text.UTF8Encoding.UTF8.GetString(byte[]))
Eric Wise wrote RE: HowTo: Create an Outlook Appointment Item in ASP .NET
on 08-26-2004 9:38 AM
Awesome, I'll try that out tonight!
Eric Wise wrote RE: HowTo: Create an Outlook Appointment Item in ASP .NET
on 08-26-2004 1:13 PM
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.
Eric Wise wrote RE: HowTo: Create an Outlook Appointment Item in ASP .NET
on 08-26-2004 1:16 PM
Ah Ha! streamWriter.AutoFlush = true;
Eric Wise wrote HowTo: Create an outlook appointment in ASP .NET Part II
on 08-26-2004 3:21 PM
HowTo: Create an outlook appointment in ASP .NET Part II
DarthPedro wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 08-27-2004 5:56 AM
I don't even program much in ASP.NET, but I thought this was very cool...
rohit wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 10-10-2004 8:28 PM
What about retrieving contacts from Outlook and Outlook Express. Is there any way I could retrieve it from an ASP.NET application?
Eric Wise wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 10-11-2004 6:49 AM
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.
sunil wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 11-10-2004 7:26 PM
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
Priya wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 07-05-2005 9:33 AM
When I try to run this code, I get an error like "Cannot import vCalendar. One or more parameter values are invalid".
Please help.
Eric Wise wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 07-09-2005 6:38 AM
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.
Jannik Nilsson wrote Working with Exchange Server and Active Directory
on 07-14-2005 1:13 AM
Do you know how to access a Calendar, User directory, appointment and so on from an Exchange Server?
Ben wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 09-23-2005 2:12 PM
Is there a comparable way to create an Outlook contact also?
Ana wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 03-23-2006 6:07 AM
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
Kyle Hancock wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 12-05-2006 9:50 AM

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?

ph wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 12-03-2007 2:12 AM

how to modify the appointment ?

phani wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 12-03-2007 2:13 AM

Ho wto modify the previously created appointment ?

Eric wrote re: HowTo: Create an Outlook Appointment Item in ASP .NET
on 03-17-2008 3:19 PM

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.

dvd decrypter software wrote dvd decrypter software
on 06-27-2008 7:06 AM

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

free dvd burning software wrote free dvd burning software
on 07-01-2008 5:23 PM

Fall back to GetProcAddress entry in another DLL version.

shrink dvd wrote shrink dvd
on 07-03-2008 10:06 AM

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

dvd decoders for xp wrote dvd decoders for xp
on 07-08-2008 6:06 AM

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

how copy dvd wrote how copy dvd
on 07-24-2008 4:06 AM

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. /

Programmatically add an appointement to the My or Exchange Calende | keyongtech wrote Programmatically add an appointement to the My or Exchange Calende | keyongtech
on 01-22-2009 5:08 AM

Pingback from  Programmatically add an appointement to the My or Exchange Calende | keyongtech