Surendra Sharma

Surendra Sharma

Search This Blog

Sunday, February 1, 2015

How to download outlook calendar file from ASP.Net website

Sometime we need to show schedule of some events on website and allow user to download the calendar for outlook which user can save in their local computer or allow them to directly open it in outlook.

The trick here is same to write the code for downloading any file from response, only thing to remember is the calendar content and content type “text/calendar”.

Here is the code

        protected void Button2_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sbICSFile = new System.Text.StringBuilder();
            DateTime dtNow = DateTime.Now;

            sbICSFile.AppendLine("BEGIN:VCALENDAR");
            sbICSFile.AppendLine("VERSION:2.0");
            sbICSFile.AppendLine("PRODID:-//ICSTestCS/");

            sbICSFile.AppendLine("BEGIN:VEVENT");
            sbICSFile.AppendLine("DTSTART:20120515T110000Z");
            sbICSFile.AppendLine("DTEND:20120515T113000Z");
            sbICSFile.AppendLine("UID:1");
            sbICSFile.AppendLine("DESCRIPTION:Test Desc");
            sbICSFile.AppendLine("SUMMARY:calander");
            sbICSFile.AppendLine("END:VEVENT");
            sbICSFile.AppendLine("END:VCALENDAR");

            Response.ContentType = "text/calendar";
            Response.AddHeader("content-disposition", "attachment; filename=CalendarEvent1.ics");
            Response.Write(sbICSFile);
            Response.Flush();
            Response.End();
        }

  
Please leave your comments or share this code if it’s useful for you.

No comments:

Post a Comment