.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DateTime >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Generating iCalender file using ASP.NET

Posted By:Mihir Soni       Posted Date: February 21, 2011    Points: 25    Category: DateTime    URL: http://www.dotnetspark.com  

Here, I am going to explain you how you can generate file format iCalander which is widely use in you calendar formats.like google calendar,outlook etc.
 

iCalender is something like an open format text file to add items to the calendar (like Outlook calendar).  It is simply a text file which contains fields and information about the particular event of a calendar.  Once you double click on the iCalendar file, the respective event gets registered in Outlook calendar (of course, with your approval for the event!).


Following is the ASP.NET code, which generates iCalendar file dynamically (on the fly) and pushes the same to user.  The user can either directly open it into Outlook or simply save the iCalendar file.



Copy following code on button click event

	StringBuilder sb = new StringBuilder(215);
	sb.AppendFormat("BEGIN:VCALENDAR{0}", Environment.NewLine);
	sb.AppendFormat("CALSCALE:GREGORIAN{0}", Environment.NewLine);
	sb.AppendFormat("VERSION:1.0{0}", Environment.NewLine);
	sb.AppendFormat("BEGIN:VEVENT{0}", Environment.NewLine);
	sb.AppendFormat("DTSTART:20080703T093000{0}", Environment.NewLine);
	sb.AppendFormat("DTEND:20080703T113000{0}", Environment.NewLine);
	sb.AppendFormat("LOCATION:testing some location{0}", Environment.NewLine);
	sb.AppendFormat("SUMMARY:Testing Some subject{0}", Environment.NewLine);
	sb.AppendFormat("CLASS:PUBLIC{0}", Environment.NewLine);
	sb.AppendFormat("END:VEVENT{0}", Environment.NewLine);
	sb.AppendFormat("END:VCALENDAR{0}", Environment.NewLine);

	UTF8Encoding enc = new UTF8Encoding();
	byte[] arrBytData = enc.GetBytes(sb.ToString);

	Response.Clear();
	Response.ContentType = "text/plain";
	Response.AppendHeader("Content-Disposition", "attachment; filename=vCalendar.ics");
	Response.AppendHeader("Content-Length", arrBytData.Length.ToString());
	Response.ContentType = "application/octet-stream";
	Response.BinaryWrite(arrBytData);
	Response.Flush();
	Response.End();
I hope this helps you in your projects

Thank you

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend