Click here to Skip to main content
11,490,282 members (38,011 online)
Click here to Skip to main content

.NET class to create and maintain vCalendar information

, 6 Feb 2003 CPOL 233K 1.2K 106
Rate this:
Please Sign up or sign in to vote.
.NET class to create and maintain vCalendar information
<!-- Article image -->

Sample Image - vCalendar.gif

<!-- Add the rest of your HTML here -->

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}", _<BR>             System.Environment.NewLine)
        'The following two lines seem to be required by <BR>        'Outlook to get the alarm settings
        result.AppendFormat("VERSION:2.0{0}", System.Environment.NewLine)
        result.AppendFormat("METHOD:PUBLISH{0}", _<BR>             System.Environment.NewLine)
        Dim item As vEvent
        For Each item In Events
            result.Append(item.ToString())
        Next
        result.AppendFormat("END:VCALENDAR{0}", _<BR>            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        <BR>           'Amount of time before event to display alarm
        Public Action As String         <BR>           'Action to take to notify user of alarm
        Public Description As String  'Description of the alarm

        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, _<BR>              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}", _<BR>               System.Environment.NewLine)
            result.AppendFormat("TRIGGER:P{0}DT{1}H{2}M{3}", _<BR>               Trigger.Days, Trigger.Hours, Trigger.Minutes, _<BR>               System.Environment.NewLine)
            result.AppendFormat("ACTION:{0}{1}", Action, _<BR>               System.Environment.NewLine)
            result.AppendFormat("DESCRIPTION:{0}{1}", _<BR>               Description, System.Environment.NewLine)
            result.AppendFormat("END:VALARM{0}", _<BR>               System.Environment.NewLine)
            Return result.ToString
        End Function
    End Class

    Public Class vEvent
        Public UID As String          'Unique identifier for the event
        Public DTStart As Date  'Start date of event.  <BR>                   'Will be automatically converted to GMT
        Public DTEnd As Date         'End date of event.  <BR>                   'Will be automatically converted to GMT
        Public DTStamp As Date        'Timestamp.  <BR>                   'Will be automatically converted to GMT
        Public Summary As String          'Summary/Subject of event
        Public Organizer As String    'Can be mailto: url or just a name
        Public Location As String
        Public Description As String
        Public URL As String
        Public Alarms As vAlarms        'Alarms needed for this event

        Public Overrides Function ToString() As String
            Dim result As New System.Text.StringBuilder()
            result.AppendFormat("BEGIN:VEVENT{0}", _<BR>               System.Environment.NewLine)
            result.AppendFormat("UID:{0}{1}", UID, _<BR>               System.Environment.NewLine)
            result.AppendFormat("SUMMARY:{0}{1}", _<BR>               Summary, System.Environment.NewLine)
            result.AppendFormat("ORGANIZER:{0}{1}", Organizer, _<BR>               System.Environment.NewLine)
            result.AppendFormat("LOCATION:{0}{1}", Location, _<BR>               System.Environment.NewLine)
            result.AppendFormat("DTSTART:{0}{1}", _<BR>               DTStart.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _<BR>               System.Environment.NewLine)
            result.AppendFormat("DTEND:{0}{1}", _<BR>               DTEnd.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _<BR>               System.Environment.NewLine)
            result.AppendFormat("DTSTAMP:{0}{1}", _<BR>               Now.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z"), _<BR>               System.Environment.NewLine)
            result.AppendFormat("DESCRIPTION:{0}{1}", Description, _<BR>               System.Environment.NewLine)
            If URL.Length > 0 Then result.AppendFormat("URL:{0}{1}", _<BR>               URL, System.Environment.NewLine)
            Dim item As vAlarm
            For Each item In Alarms
                result.Append(item.ToString())
            Next
            result.AppendFormat("END:VEVENT{0}", _<BR>                System.Environment.NewLine)
            Return result.ToString
        End Function

        Public Sub New()
            Me.Alarms = New vAlarms()
        End Sub
    End Class

    Public Class vAlarms
        ' The first thing to do when building a CollectionBase <BR>        ' class is to inherit from System.Collections.CollectionBase
        Inherits System.Collections.CollectionBase

        Public Overloads Function Add(ByVal Value As vAlarm) As vAlarm
            ' After you inherit the CollectionBase class, you <BR>            ' can access an intrinsic object
            ' called InnerList that represents your collection. <BR>            ' InnerList is of type ArrayList.
            Me.InnerList.Add(Value)
            Return Value
        End Function

        Public Overloads Function Item(ByVal Index As Integer) As vAlarm
            ' To retrieve an item from the InnerList, <BR>            ' pass the index of that item to the .Item property.
            Return CType(Me.InnerList.Item(Index), vAlarm)
        End Function

        Public Overloads Sub Remove(ByVal Index As Integer)
            ' This Remove expects an index.
            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
        ' The first thing to do when building a CollectionBase <BR>        ' class is to inherit from System.Collections.CollectionBase
        Inherits System.Collections.CollectionBase

        Public Overloads Function Add(ByVal Value As vEvent) As vEvent
            ' After you inherit the CollectionBase class, <BR>            ' you can access an intrinsic object
            ' called InnerList that represents your collection. <BR>            ' InnerList is of type ArrayList.
            Me.InnerList.Add(Value)
            Return Value
        End Function

        Public Overloads Function Item(ByVal Index As Integer) As vEvent
            ' To retrieve an item from the InnerList, <BR>            ' pass the index of that item to the .Item property.
            Return CType(Me.InnerList.Item(Index), vEvent)
        End Function

        Public Overloads Sub Remove(ByVal Index As Integer)
            ' This Remove expects an index.
            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):

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

slolife
Web Developer
United States United States
http://www.onlinescorekeeper.com/

Comments and Discussions

 
RantNice work but are missing a couple of things like vAttendee and vAttach.. Pin
mbastianon3-Aug-10 7:08
membermbastianon3-Aug-10 7:08 
GeneralMy vote of 2 Pin
Dave Kreskowiak2-Apr-10 7:53
mvpDave Kreskowiak2-Apr-10 7:53 
GeneralVishal Sanchihar Pin
Prakshupa16-Sep-09 22:23
memberPrakshupa16-Sep-09 22:23 
QuestionMark as a private event? Pin
neilio24-Aug-09 15:06
memberneilio24-Aug-09 15:06 
AnswerRe: Mark as a private event? Pin
slolife27-Aug-09 11:10
memberslolife27-Aug-09 11:10 
GeneralRe: Mark as a private event? Pin
mbielski16-Mar-11 10:20
membermbielski16-Mar-11 10:20 
GeneralRe: Mark as a private event? Pin
slolife9-Jun-11 13:39
memberslolife9-Jun-11 13:39 
QuestionHTML in vCalendar? Pin
Ricardo Cohen3-Jul-09 9:13
memberRicardo Cohen3-Jul-09 9:13 
AnswerRe: HTML in vCalendar? Pin
slolife6-Jul-09 11:33
memberslolife6-Jul-09 11:33 
QuestionCreate an iCal Feed ? Pin
Jenyphur11-Sep-08 14:58
memberJenyphur11-Sep-08 14:58 
AnswerRe: Create an iCal Feed ? Pin
kmazur1111-Oct-08 21:31
memberkmazur1111-Oct-08 21:31 
GeneralAutomatically add to calendar Pin
Danko Greiner28-Mar-08 6:30
memberDanko Greiner28-Mar-08 6:30 
GeneralNot Working Pin
samiji27-Mar-08 2:31
membersamiji27-Mar-08 2:31 
GeneralRe: Not Working Pin
slolife28-Mar-08 14:37
memberslolife28-Mar-08 14:37 
AnswerRe: Not Working Pin
jeremypettit5-May-08 10:22
memberjeremypettit5-May-08 10:22 
GeneralAuto-Updates Pin
justin.moses6-Feb-08 14:08
memberjustin.moses6-Feb-08 14:08 
GeneralRecurring Events Pin
MeDeeSa24-Jan-07 12:03
memberMeDeeSa24-Jan-07 12:03 
GeneralRe: Recurring Events Pin
kmazur1111-Oct-08 21:30
memberkmazur1111-Oct-08 21:30 
GeneralRe: Recurring Events Pin
MeDeeSa13-Oct-08 13:29
memberMeDeeSa13-Oct-08 13:29 
Generalinseting image in vcalendar Pin
Rajesh Sadashivan9-Nov-06 22:57
memberRajesh Sadashivan9-Nov-06 22:57 
GeneralRe: inseting image in vcalendar Pin
Chris Becker12-Nov-06 6:31
memberChris Becker12-Nov-06 6:31 
Generalcalendar time not seeming to work Pin
Stacey Levine11-Aug-06 9:45
memberStacey Levine11-Aug-06 9:45 
GeneralRe: calendar time not seeming to work Pin
Chris Becker11-Aug-06 11:15
memberChris Becker11-Aug-06 11:15 
GeneralExample of how I used the class for Outlook Pin
-Travis15-Feb-06 5:26
member-Travis15-Feb-06 5:26 
Generalcalendar Pin
M.Mary Joancy8-Jan-06 6:09
memberM.Mary Joancy8-Jan-06 6:09 
GeneralRe: calendar Pin
Chris Becker8-Jan-06 18:56
memberChris Becker8-Jan-06 18:56 
Generalcalendar Pin
M.Mary Joancy8-Jan-06 6:07
memberM.Mary Joancy8-Jan-06 6:07 
GeneralvCalendar in windows application Pin
Titan Williams22-Jun-05 23:33
memberTitan Williams22-Jun-05 23:33 
GeneralNice Pin
killerKiwinz2729-May-05 18:11
memberkillerKiwinz2729-May-05 18:11 
QuestionHow do you use it? Pin
Ian Powell19-Jan-05 3:33
sussIan Powell19-Jan-05 3:33 
AnswerRe: How do you use it? Pin
Ian Powell19-Jan-05 5:56
sussIan Powell19-Jan-05 5:56 
GeneralRe: How do you use it? Pin
BossDK18-May-05 3:44
memberBossDK18-May-05 3:44 
GeneralRe: How do you use it? Pin
Ian Powell18-May-05 6:13
memberIan Powell18-May-05 6:13 
QuestionPlease show me on how to use it? Pin
mcflee8-Nov-04 6:02
membermcflee8-Nov-04 6:02 
GeneralThe iCalendar Pin
Anthony_Yio6-Oct-04 22:57
memberAnthony_Yio6-Oct-04 22:57 
GeneralRecurring event Pin
Rolfpants3-Sep-04 4:20
memberRolfpants3-Sep-04 4:20 
GeneralRe: Recurring event Pin
Chris Becker3-Sep-04 6:43
memberChris Becker3-Sep-04 6:43 
GeneralStandards? Hah! Pin
Brian Lowe11-May-04 4:12
memberBrian Lowe11-May-04 4:12 
QuestionHow to create it as email via SMTP Pin
eric.martin5-Apr-04 14:01
susseric.martin5-Apr-04 14:01 
AnswerRe: How to create it as email via SMTP Pin
Chris Becker5-Apr-04 14:18
sussChris Becker5-Apr-04 14:18 
GeneralNot working Pin
mmikey12125-Mar-04 8:17
membermmikey12125-Mar-04 8:17 
GeneralRe: Not working Pin
Chris Becker25-Mar-04 8:45
sussChris Becker25-Mar-04 8:45 
GeneralRe: Not working Pin
mmikey12125-Mar-04 9:40
membermmikey12125-Mar-04 9:40 
GeneralRe: Not working Pin
Chris Becker25-Mar-04 9:50
sussChris Becker25-Mar-04 9:50 
GeneralRe: Not working Pin
mmikey12125-Mar-04 10:45
membermmikey12125-Mar-04 10:45 
Thank you very much.

Is there an advantage of ics vs. vcs format?


GeneralRe: Not working Pin
Chris Becker25-Mar-04 11:17
sussChris Becker25-Mar-04 11:17 
GeneralRe: Not working Pin
mmikey12125-Mar-04 10:52
membermmikey12125-Mar-04 10:52 
GeneralRe: Not working Pin
Chris Becker25-Mar-04 11:25
sussChris Becker25-Mar-04 11:25 
GeneralRe: Not working Pin
mmikey12125-Mar-04 11:29
membermmikey12125-Mar-04 11:29 
GeneralRe: Not working Pin
Chris Becker25-Mar-04 11:37
sussChris Becker25-Mar-04 11:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 7 Feb 2003
Article Copyright 2003 by slolife
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid