none
Help with creating a .ics file RRS feed

  • Question

  • I have a two questions here. I  tried to create a .ics file to save events to outlook calendar. The calendar has multiple events within one day. First,  I tried to loop through to write the events to the .ics file but got error message said it was not a valid internet calendar file format. It was fine if there was only one event. Second, the start time and end time always 4 hours ahead when it opens the outlook calendar (ie. 11am to 11:45am turns to 7am to 7:45am).   I am in Eastern time zone and the server is also in the same location. I don't know how to offset the 4 hours? And oo I need to use millitary time format for the afternoon hours? TIA.

    My Handler.ashx.cs code is as below:

     public void ProcessRequest(HttpContext context) 
            { 
                string strConn = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
                SqlConnection conn = new SqlConnection(strConn); 
                conn.Open(); 
                string strSql = string.Empty; 
                SqlCommand cmd; 
                string Lplannerstr = string.Empty; 
                if (outlookcal.plannerstr.Length > 0) 
                { 
                    //trim the last comma 
                    Lplannerstr = outlookcal.plannerstr.Substring(0, outlookcal.plannerstr.Length - 1); 
                     
                } 
                strSql = "select * from proposals where id in ('" +Lplannerstr.Replace(",","','")+ "')"; 
                cmd = new SqlCommand(strSql, conn); 
                SqlDataReader dr = cmd.ExecuteReader(); 
               
                    
                    context.Response.Clear(); 
                     
                    context.Response.ContentType = "text/calendar"; 
                  
                    context.Response.AddHeader("Content-Disposition", "inline; filename=\\Calender.ics"); 
                context.Response.BinaryWrite(new System.Text.ASCIIEncoding().GetBytes(@"BEGIN:VCALENDAR"+Environment.NewLine)); 
                context.Response.Write("PRODID: -//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN" + Environment.NewLine); 
     
                     context.Response.Write("VERSION:1.0" + Environment.NewLine); 
                      
     
                while (dr.Read()) 
                    { 
                        
                     
     
                        context.Response.Write("BEGIN:VEVENT" + Environment.NewLine); 
     
     
                        context.Response.Write("DTSTART: 20120905T" + dr["session_start"] + "00Z" + Environment.NewLine); 
                       
                         
     
     
     
                        context.Response.Write("DESCRIPTION:" + dr["session_title"] + Environment.NewLine); 
     
                        context.Response.Write("DTEND:20120905T" + dr["session_end"] + "00Z" + Environment.NewLine); 
                         
     
                        context.Response.Write("LOCATION:" +dr["session_room"]+ Environment.NewLine); 
                        
     
                      
     
                        context.Response.Write("UID:040000008200E00074C5B7101A82E00800000000E09778CC8A2FCD01000000000000000"+Environment.NewLine); 
                        context.Response.Write("010000000A616AC829938554DA102762D344287C7" + Environment.NewLine); 
                        context.Response.Write("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:==0D=0A" + Environment.NewLine); 
                        context.Response.Write("SUMMARY:PUP Conference" + Environment.NewLine); 
                        context.Response.Write("PRIORITY:3" + Environment.NewLine); 
                     
     
     
    context.Response.Write("END:VEVENT" + Environment.NewLine); 
    context.Response.Write("END:VCALENDAR" + Environment.NewLine); 
                     
     
                    } 
                 
              
                context.Response.End(); 
                dr.Dispose(); 
                cmd.Dispose(); 
                conn.Close(); 
            }

    Friday, September 14, 2012 1:55 PM

Answers

  • You will need to specify the time in the ICS file and make sure all the date/time values in the file reference that time zone.

    Try to create an ICS file from Outlook and compare it with what your code is creating.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Friday, September 14, 2012 2:38 PM
  • Never mind. I found the answer already.
    Friday, September 14, 2012 5:36 PM

All replies