Tip: Generating iCalender file using ASP.NET

 
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.
 

Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sb As 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)

 

        Dim enc As New UTF8Encoding

        Dim arrBytData() As Byte = 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()

    End Sub

End Class

 
thanks
Jag

About Jag

.NET Architect
This entry was posted in ASP.NET and tagged , . Bookmark the permalink.