Introduction
I have been porting the calendar portion of my site to ASP.NET and came to the exporting of event information to a vCalendar file that can be imported into Outlook or other calendar apps that support the vCalendar/iCalendar format. So I created a class to wrap all of the formatting of information.
Public Class vCalendar
Public Events As vEvents
Public Overrides Function ToString() As String
Dim result As New System.Text.StringBuilder()
result.AppendFormat("BEGIN:VCALENDAR{0}", _
System.Environment.NewLine)
result.AppendFormat("VERSION:2.0{0}", System.Environment.NewLine)
result.AppendFormat("METHOD:PUBLISH{0}", _
System.Environment.NewLine)
Dim item As vEvent
For Each item In Events
result.Append(item.ToString())
Next
result.AppendFormat("END:VCALENDAR{0}", _
System.Environment.NewLine)
Return result.ToString
End Function
Public Sub New(ByVal Value As vEvent)
Me.Events = New vEvents()
Me.Events.Add(Value)
End Sub
Public Sub New()
Me.Events = New vEvents()
End Sub
Public Class vAlarm
Public Trigger As TimeSpan
Public Action As String
Public Description As String
Public Sub New()
Trigger = TimeSpan.FromDays(1)
Action = "DISPLAY"
Description = "Reminder"
End Sub
Public Sub New(ByVal SetTrigger As TimeSpan)
Trigger = SetTrigger
Action = "DISPLAY"
Description = "Reminder"
End Sub
Public Sub New(ByVal SetTrigger As TimeSpan, _
ByVal SetAction As String, ByVal SetDescription As String)
Trigger = SetTrigger
Action = SetAction
Description = SetDescription
End Sub
Public Overrides Function ToString() As String
Dim result As New System.Text.StringBuilder()
result.AppendFormat("BEGIN:VALARM{0}", _
System.Environment.NewLine)
result.AppendFormat("TRIGGER:P{0}DT{1}H{2}M{3}", _
Trigger.Days, Trigger.Hours, Trigger.Minutes, _
System.Environment.NewLine)
result.AppendFormat("ACTION:{0}{1}", Action, _
System.Environment.NewLine)
result.AppendFormat("DESCRIPTION:{0}{1}", _
Description, System.Environment.NewLine)
result.AppendFormat("END:VALARM{0}", _
System.Environment.NewLine)
Return result.ToString
End Function
End Class
Public Class vEvent
Public UID As String
Public DTStart As Date
Public DTEnd As Date
Public DTStamp As Date
Public Summary As String
Public Organizer As String
Public Location As String
Public Description As String
Public URL As String
Public Alarms As vAlarms
Public Overrides Function ToString() As String
Dim result As New System.Text.StringBuilder()
result.AppendFormat("BEGIN:VEVENT{0}", _
System.Environment.NewLine)
result.AppendFormat("UID:{0}{1}", UID, _
System.Environment.NewLine)
result.AppendFormat("SUMMARY:{0}{1}", _
Summary, System.Environment.NewLine)
result.AppendFormat("ORGANIZER:{0}{1}", Organizer, _
System.Environment.NewLine)
result.AppendFormat("LOCATION:{0}{1}", Location, _
System.Environment.NewLine)
result.AppendFormat("DTSTART:{0}{1}", _
DTStart.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _
System.Environment.NewLine)
result.AppendFormat("DTEND:{0}{1}", _
DTEnd.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _
System.Environment.NewLine)
result.AppendFormat("DTSTAMP:{0}{1}", _
Now.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _
System.Environment.NewLine)
result.AppendFormat("DESCRIPTION:{0}{1}", Description, _
System.Environment.NewLine)
If URL.Length > 0 Then result.AppendFormat("URL:{0}{1}", _
URL, System.Environment.NewLine)
Dim item As vAlarm
For Each item In Alarms
result.Append(item.ToString())
Next
result.AppendFormat("END:VEVENT{0}", _
System.Environment.NewLine)
Return result.ToString
End Function
Public Sub New()
Me.Alarms = New vAlarms()
End Sub
End Class
Public Class vAlarms
Inherits System.Collections.CollectionBase
Public Overloads Function Add(ByVal Value As vAlarm) As vAlarm
Me.InnerList.Add(Value)
Return Value
End Function
Public Overloads Function Item(ByVal Index As Integer) As vAlarm
Return CType(Me.InnerList.Item(Index), vAlarm)
End Function
Public Overloads Sub Remove(ByVal Index As Integer)
Dim cust As vAlarm
cust = CType(Me.InnerList.Item(Index), vAlarm)
If Not cust Is Nothing Then
Me.InnerList.Remove(cust)
End If
End Sub
End Class
Public Class vEvents
Inherits System.Collections.CollectionBase
Public Overloads Function Add(ByVal Value As vEvent) As vEvent
Me.InnerList.Add(Value)
Return Value
End Function
Public Overloads Function Item(ByVal Index As Integer) As vEvent
Return CType(Me.InnerList.Item(Index), vEvent)
End Function
Public Overloads Sub Remove(ByVal Index As Integer)
Dim cust As vEvent
cust = CType(Me.InnerList.Item(Index), vEvent)
If Not cust Is Nothing Then
Me.InnerList.Remove(cust)
End If
End Sub
End Class
End Class
I plan to add more properties as I discover them, but these are pretty much the only ones that are supported by outlook at this time. I also wanted to get some feedback on my design.
vCalendar specs
Here are some links to for the vCalendar specs (thanks Tommi):