ICAL.NET Updating Events not working using outlook. VB

354 Views Asked by At
 I am trying to create a calendar file (.ics) using ical.net in my vb application.  My application requires appointments to be approved first.  On appointment creation they are marked as pending, then when approved, it updates to Approved. 

 I have no issue creating the appointment or cancelling the appointment, just updating it.  My appointments have both required and optional attendees.  I can't figure out how to get this to work.  My update ics file just adds it as a new appointment.  The UID is the same in both files.  

 I think it has something to do with METHOD maybe, not sure.  If I use PUBLISH, I can add it to my outlook calendar when I open the ICS file, but in the update ics file it just adds it as new.  If I use REQUEST, it doesn't get added to my calendar.  When I open it, it just has the remove option.  These files will ONLY be used in outlook.

Here is my code to generate the ics file:

    Public Shared Function createoutlookappt(ByVal br As benchrequest, Optional ByVal type As String = Nothing) As String
    Dim recurrence As RecurrencePattern = Nothing
    If Not String.IsNullOrEmpty(br.recurrinfo) Then
        Dim helper As RecurrenceInfoXmlPersistenceHelper = New RecurrenceInfoXmlPersistenceHelper(New RecurrenceInfo())
        Dim recInfo As RecurrenceInfo = CType(helper.FromXml(br.recurrinfo), RecurrenceInfo)

        recurrence = New RecurrencePattern With {
        .FirstDayOfWeek = recInfo.FirstDayOfWeek,
        .Frequency = CType([Enum].Parse(GetType(FrequencyType), recInfo.Type.ToString), FrequencyType),
        .Count = recInfo.OccurrenceCount,
        .Until = recInfo.End,
        .Interval = recInfo.Periodicity
        }

    End If

    Dim e = New CalendarEvent

    With e
        .Start = New CalDateTime(CDate(br.start_time))
        .[End] = New CalDateTime(CDate(br.end_time))
        .DtStamp = New CalDateTime(DateTime.Now)
        .Location = br.bench_name
        .Priority = br.priority
        .Uid = br.uid.ToString
        .Sequence = CInt(br.seq)
        If Not String.IsNullOrEmpty(br.allday) Then
            .IsAllDay = CBool(br.allday)
        End If
        .LastModified = New CalDateTime(Now)
        .Description = br.notes
        .Summary = "(" & br.labeltxt & ") Bench Request (ID-" & br.request_id & "): " & br.program_name & " - " & br.project_name & " - " & br.activity
        .RecurrenceRules = New List(Of RecurrencePattern)() From {recurrence}
        .Class = "PUBLIC"
        .Transparency = TransparencyType.Opaque
        .Organizer = New Organizer() With {.Value = New Uri("mailto:" & br.requesting_user_email)}
    End With

    'Add attendees
    Dim dt As New DataTable
    Dim ta As New BSDataSetTableAdapters.getUserRequestTableAdapter
    dt = ta.GetData(br.request_id, "T")

    Dim attendee As Attendee

    For Each row As DataRow In dt.Rows
        If row("type") = "Required" Then
            attendee = New Attendee With {
            .CommonName = (row("last_name") & ", " & row("first_name")).ToString,
            .Rsvp = True,
            .Value = New Uri("mailto:" & row("email").ToString),
            .Role = "REQ-PARTICIPANT"
            }
            e.Attendees.Add(attendee)
        ElseIf row("type") = "Optional" Then
            attendee = New Attendee With {
            .CommonName = (row("last_name") & ", " & row("first_name")).ToString,
            .Rsvp = False,
            .Role = "OPT-PARTICIPANT",
            .Value = New Uri("mailto:" & row("email").ToString)
            }
            e.Attendees.Add(attendee)
        End If
    Next

    Dim calendar = New Calendar()

    Select Case type
        Case "Deny"
            calendar.Method = "CANCEL"
        Case "Approved"
            calendar.Method = "PUBLISH"
        Case "New Request"
            calendar.Method = "PUBLISH"
        Case Else
            calendar.Method = "PUBLISH"
    End Select

    calendar.Events.Add(e)
    Dim serializer = New CalendarSerializer()
    Dim serializedCalendar = serializer.SerializeToString(calendar)

    Dim bytesCalendar = Encoding.ASCII.GetBytes(serializedCalendar)
    Dim ms As MemoryStream = New MemoryStream(bytesCalendar)

    Dim filename As String = Path.GetTempPath & br.request_id & ".ics"
    Dim file As New FileStream(filename, FileMode.Create, FileAccess.Write)

    ms.WriteTo(file)

    file.Close()
    ms.Close()

    Return filename
End Function

This is the initial ics file for creating the meeting:

BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//github.com/rianjs/ical.net//NONSGML ical.net 4.0//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN="K, David";RSVP=TRUE;ROLE=REQ-PARTICIPANT:mailto:[email protected]
CLASS:PUBLIC
DESCRIPTION:testing ical
DTEND:20220406T110000
DTSTAMP:20220323T115745
DTSTART:20220406T080000
LAST-MODIFIED:20220323T115745
LOCATION:Test Bench 1
ORGANIZER:mailto:[email protected]
PRIORITY:3
RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=20220525T110000;WKST=SU;COUNT=4
SEQUENCE:0
SUMMARY:(PENDING) Bench Request (ID-35): Dave Test Program - Test Project
  - Bench Maintainance
TRANSP:OPAQUE
UID:ddc29fe7-12bf-4f7d-bb13-48989433c605
END:VEVENT
END:VCALENDAR

This is my update file output:

BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//github.com/rianjs/ical.net//NONSGML ical.net 4.0//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN="K, David";RSVP=TRUE;ROLE=REQ-PARTICIPANT:mailto:[email protected]
CLASS:PUBLIC
DTEND:20220406T110000
DTSTAMP:20220323T120932
DTSTART:20220406T080000
LAST-MODIFIED:20220323T120932
LOCATION:Test Bench 1
ORGANIZER:mailto:[email protected]
PRIORITY:0
RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=20220525T110000;WKST=SU;COUNT=4
SEQUENCE:0
SUMMARY:(Approved) Bench Request (ID-35): Dave Test Program - Test Project
  - Bench Maintainance
TRANSP:OPAQUE
UID:ddc29fe7-12bf-4f7d-bb13-48989433c605
END:VEVENT
END:VCALENDAR

This is my cancel file output:

BEGIN:VCALENDAR
METHOD:CANCEL
PRODID:-//github.com/rianjs/ical.net//NONSGML ical.net 4.0//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN="K, David";RSVP=TRUE;ROLE=REQ-PARTICIPANT:mailto:[email protected]
CLASS:PUBLIC
DTEND:20220406T110000
DTSTAMP:20220323T121655
DTSTART:20220406T080000
LAST-MODIFIED:20220323T121655
LOCATION:Test Bench 1
ORGANIZER:mailto:[email protected]
PRIORITY:0
SEQUENCE:1
SUMMARY:(Denied) Bench Request (ID-35): Dave Test Program - Test Project -
TRANSP:OPAQUE
UID:ddc29fe7-12bf-4f7d-bb13-48989433c605
END:VEVENT
END:VCALENDAR

What am I doing wrong. I have search and read many different posts and alike and nothing has worked.

My end goal. A user creates an appointment in my application where it is marked as pending. A ics file is created where they can add the appointment to their outlook calendar. An email is generated to the approvers. Once the manger goes in an approves it, an email is sent to the user letting them know it is approved (or denied) with an ics file attachment to update the appointment on their calendar (or remove it if denied). There could also be a situation where the date, time changes, so I would need to have those updates as well.

Sorry for the long post, wanted to provide as much info as possible. Thanks in advance

0

There are 0 best solutions below