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:
CODE:
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();
}
1 comments:
bogus it's not working
Post a Comment