Blog Navigation
Partners
Latest Activity
Phil gives the source code for implementing a MySQL singleton class in both PHP and Python.
Exchange 2010 Calendar to ICalendar using ASP.net / C# – Coding Blog 2/2
Reminders: As with anything I post, I ask that you provide a backlink to my code if you use it (if you don’t, its considered plagiarism). Also, the code below is the outcome of 2 days in ASP.net as an excursion, I’m sure that my code could be improved vastly. I’m also not going to baby step through this, so you’re going to have to be a somewhat experienced programmer to follow along with the rest of my blog. One last thing: If you feel the urge to donate to me for my time (provided this solution helped you), please use the NEW donate button in the footer, I appreciate it.
In Visual Studio 2010, create a new empty website in C#. I’d recommend giving this website a logical name, mine is named VCalendarGenerator. Once you’re settled, right click the actual website and goto “add service reference”. In the address box, you want to enter the service reference Address, which is the path to exchange.asmx. This will be something like https://
Now we’re all set up to program in Visual Studio. Since I want the Default page of the website to load a VCalendar (I don’t want this website to do anything else), I’m going to right click the project and add a new item. The item that I want to add is a web form. This should be bring up a new page in the list called Default.aspx. Now we want to right click on Default.aspx and select view code. Within the partial class scope, you want to insert the following function:
/**
* Description: Prints a VCalendar Complaint File for web distribution from an Exchange 2010 Account - TESTED
* This file accompanies the FamousPhil.com Blog Post regarding ASP.net and Exchange -> VCalendar (August 2010)
* NOTE: Written on ASP.NET Framework 4.0
* NOTE: Here are the proper imports for your class file (usually placed above the class name)
using System;
using ewsclass;
using System.Net;
* NOTE: Page that this function prints to MUST contain no additional content
Recommended Display Page code:
<%@ Language="C#" CodeFile="VCalendar.aspx.cs" Inherits="VCalendar" %>
<% GenerateVCalendar(); %>
*
* Accepts the following parameters:
*
* User - Exchange OWA Username (without the domain)
* Password - Exchange OWA Password for Username Specified
* fqdn - Fully Qualified Domain Name (OWA Access Top Level Domain like yahoo.com)
* esburl - Exchange Web Services URL - Normally https://<fqdn>/ews/exchange.asmx
* datestart - Integer for how many days from today to start outputing calendar events (-30 would mean from 30 days ago)
* dateend - Integer for how many days from today to end output calendar events (30 would mean 30 days from today into the future)
*
* To minimize Exchange load and time outs (and slow load speeds), keep the date range under 360 days (a full year)
*/
public void GenerateVCalendar(String user, String password, String fqdn, String esburl, int datestart, int dateend)
{
Response.ContentType = "text/calendar";
Response.AddHeader("Content-disposition", "attachment; filename=calendar.ics");
Response.Write("BEGIN:VCALENDAR");
Response.Write(System.Environment.NewLine);
Response.Write("PRODID:-//Matthouse.us//Matthouse.us / Famousphil.com Exchange to VCAL v1.00//EN");
Response.Write(System.Environment.NewLine);
Response.Write("VERSION:2.0");
Response.Write(System.Environment.NewLine);
Response.Write("CALSCALE:GREGORIAN");
Response.Write(System.Environment.NewLine);
Response.Write("METHOD:PUBLISH");
Response.Write(System.Environment.NewLine);
Response.Write("X-WR-CALNAME:Exported Exchange Calendar For user:"+user);
Response.Write(System.Environment.NewLine);
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential(user, password, fqdn);
esb.Url = esburl;
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
//Calendar extraction code partially found from
//http://weblogs.asp.net/psperanza/archive/2008/03/18/getting-calendar-items-using-exchange-web-services.aspx
FindItemType findItemRequest = new FindItemType();
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = DateTime.Now.AddDays(datestart);
calendarView.EndDate = DateTime.Now.AddDays(dateend);
calendarView.MaxEntriesReturned = 5000;
calendarView.MaxEntriesReturnedSpecified = true;
findItemRequest.Item = calendarView;
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties;
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
findItemRequest.ParentFolderIds = folderIDArray;
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
try
{
FindItemResponseType findItemResponse =
esb.FindItem(findItemRequest);
ArrayOfResponseMessagesType responseMessages =
findItemResponse.ResponseMessages;
ResponseMessageType[] rmta = responseMessages.Items;
int folderNumber = 0;
foreach (ResponseMessageType rmt in rmta)
{
FindItemResponseMessageType firmt =
rmt as FindItemResponseMessageType;
if (firmt.RootFolder == null)
continue;
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items =
(obj as ArrayOfRealItemsType);
if (items.Items == null)
{
folderNumber++;
}
else
{
foreach (ItemType it in items.Items)
{
if (it is CalendarItemType)
{
CalendarItemType cal = (CalendarItemType)it;
Response.Write("BEGIN:VEVENT");
Response.Write(System.Environment.NewLine);
Response.Write("UID:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + "@"+fqdn);
Response.Write(System.Environment.NewLine);
Response.Write("DTSTAMP:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
Response.Write(System.Environment.NewLine);
Response.Write("DTSTART:" + cal.Start.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
Response.Write(System.Environment.NewLine);
Response.Write("DTEND:" + cal.End.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
Response.Write(System.Environment.NewLine);
Response.Write("SUMMARY:" + cal.Subject);
Response.Write(System.Environment.NewLine);
Response.Write("LOCATION:" + cal.Location);
Response.Write(System.Environment.NewLine);
Response.Write("DESCRIPTION:" + cal.Body);
Response.Write(System.Environment.NewLine);
Response.Write("END:VEVENT");
Response.Write(System.Environment.NewLine);
}
}
folderNumber++;
}
}
}
}
catch (Exception e)
{
throw;
}
finally
{
}
Response.Write(System.Environment.NewLine);
Response.Write("END:VCALENDAR");
Response.Write(System.Environment.NewLine);
}
Save the default.aspx.cs file. Let’s open up Default.aspx now and remove all the default HTML and body ASP code (mostly forms) that is present in that file except for the top line that starts with <% (which is the asp tag). We’re going to replace this with:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% GenerateVCalendar(); %>
If you know anything about code, you should realize that this is an improper call. Using the parameters defined in the block comment above my code, fill in this function appropriately. Once everything looks good, right click your project and publish it to an IIS server. I had a few difficulties making it work with IIS at first, but those were all solved after I reinstalled the .NET Framework 4 update on my IIS server (which I consider to be an administration issue, not a developer issue).
I’ve decided against posting my full Visual Studio project online because I’ve already given you 95% of what you need to make this work. I’ve discovered that programmers who are learning like to copy paste whatever code claims to do what they want without looking, and this leads to incompetent programmers! Experienced / intermediate coders (with little ASP.NET experience) will immediately understand what I’m saying and fill in the pieces quickly, and that is the goal of this blog post.
One final note about FamousPhil’s recent design change: I will soon release the new calendar to my site (making the calendar icon work) sometime this weekend. My goal is to rewrite the code above into a local application which can be run from the command line to use fewer server resources. I also see some other issues with the header not working on Internet Explorer, so I will soon be fixing that also.
Tags: ASP.net, c#, convert, exchange 2010, extract, ical, icalendar, ics, VCAL, vcalendar, visual studio
Posted in My Site, Programming
Great article. I aggre with you. In some free time please take look on my personal website.
Exceptional post, hold it up
Great thoughts on computers. Excited to have discovered your blog and add it to my reader. Cheers!
Good to become going to your weblog once again, it’s been months for me. Well this content that i’ve been waited for so extended. I have to have this post to total my assignment in the college, and it’s very same topic along with your article. Many thanks, excellent share.
Helpful, book-marked, thanks a ton.
Exciting website. Understand a number of other of one’s blogged material and I’ve to say it is getting a day-to-day habit of mine to maintain returning back again trying to uncover new material lol. Preserve up the excellent function.
Great things from you, guy. Ive go through your material before and youre just too great. I enjoy what youve obtained listed here, appreciate what youre expressing and the way you say it. You allow it to be amusing and also you nevertheless take care of to maintain it smart. I cant wait to study additional from you. This can be genuinely a terrific website.
This is a good article. We’re always looking for relevant resources to send to the retirement community, and your post is without a doubt worth sharing!
Ha!Hey!Hah!. That’s amusing. That’s the fifth time I’ve foundsomething like that. interesting.
Hey, awesome post, but WordPress messes it up on my computer. Maybe it’s the widget you are using. Have you throught about Drupal?
Finally, I located the information I was looking for. I have been doing research on this subject, and for two days I keep entering sites that are supposed to have what I am searching for, only to be disappointed with the lack of what I needed. I wish I would have located your website sooner! I had about 25% of what I needed and your website has that, and the rest of what I needed to finish my research. Thank you and I will report back on how it goes!
I was searching for this information for long time, thanks for penning this article.
I LOVE THIS BLOG!!!
I’ve read over most of your entries and I was curious about if you were interesting in swapping webpage links? I am usually hunting to switch links with blogs about similar topics! I look forward to hearing back from you in the near future.
i really like your blog do u write your own posts or you outsource the work to someone else. because i have a couple of blogs and i have a really hard time updating all of them.
Thanks
Thank you! That’s a perfect sample, it helped me a lot!
Wonderful thank you for the information. A Exceptional piece of knowledge.
good read, appreciate it. always seeking out for unusual and informative threads to read
This is great! I have been trying to figure this stuff out for a while now (including the last 4 hours)!
I am still getting an error when I try and call the function… Any help would be appreciated.
I’ve sent an email privately to you. It might be an issue with the code and how its connecting or it could be a silly mistake.
Great code and great blog. Bookmarked!
Phil,
I am investigating the possibility of importing from an ICalendar file to Exchange/Outlook.
New appointments are fine, I am running into a dead end with updates and deletes.
Would appreciate any insight if you have some to share on the issue.
Exporting an ICal became much easier in Exchange 2010 SP1, so good that my website calendar is now entirely powered by Exchange for the public to see. Importing a calendar has always been as easy as going to your calendar and adding a calendar. You can mix it into your existing calendar using OWA directly (I just verified). If you’re looking to merge the calendars, I might be able to help you out some more, but I’d ask that you email me directly using my contact page.
Thanks Phil, I really appreciate the free advice.
I did find a solution to my problem. The answer lay in publishing the calendar using the same value for the “X-WR-RELCALID” attribute. My earlier ICS did not use this attribute.
Once included, Outlook simply replaced the existing calendar with the new calendar. This resolved my issue with deleted appointmrnts & updates neatly.
In case this interests someone, the page below has a sample ICS that can be used as the template for anyone interested in generating their own calendars.
http://msdn.microsoft.com/en-us/library/ee160658(v=EXCHG.80).aspx
Finally, I located the information I was looking for. I have been doing research on this subject, and for two days I keep entering sites that are supposed to have what I am searching for, only to be disappointed with the lack of what I needed.
A Exceptional piece of knowledge.