Listing 1: Startup event

Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        'Load the settings
        Me._settings = New Settings().Load(False)

        'Run the settings changed handler to get the state setup
        Me.SettingsChangedHandler()

        'Hook the settings changed event for future changes.
        AddHandler _settings.Changed, AddressOfSettingsChangedHandler

        'Startup the processors
        Me._calProcessor = New CalendarProcessor(Me, Me._settings)

        'Show the menu
        Me.InitializeMenu()
End Sub


Listing 2: Hooking the Outlook events so we're notified of changes to our calendar

Protected Overrides Sub HookOutlookEvents()
        'Get a handle to the calendar folder in outlook.
        _calendarFolder = 
Me._app.ActiveExplorer().Session.GetDefaultFolder (Outlook.OlDefaultFolders.olFolderCalendar)
        _calendarItems = _calendarFolder.Items

        'Get a handle to the deleted items folder in outlook.
        _deletedItemsFolder = 
Me._app.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderDeletedItems)
        _deletedItems = _deletedItemsFolder.Items

        'Sink the events so we get notified.
        AddHandler _calendarItems.ItemAdd, AddressOfItemAddHandler
        AddHandler _calendarItems.ItemChange, AddressOf ItemChangeHandler
        AddHandler _deletedItems.ItemAdd, AddressOfItemRemoveHandler

End Sub

Listing 3: Adding an item to the remote Exchange server

Private Sub AddNewItem(ByVal newAppointment As Outlook.AppointmentItem)
        Dim newRemoteAppointment As Appointment = NewAppointment

        Me.CopyAttributes(newAppointment, newRemoteAppointment)

        'Required to get cal items to display properly in Outlook
        Dim customProperty() As Independentsoft.Webdav.Exchange.Property = 
		New Independentsoft.Webdav.Exchange.Property(0) {}

        Dim dispidRecurring As String
        If newAppointment.IsRecurring Then
            dispidRecurring = "1"
        Else
            dispidRecurring = "0"
        End If

        customProperty(0) = New Independentsoft.Webdav.Exchange.Property(dispidRecurring)
        Dim multiStatus As MultiStatus = Me._app.ExchangeResource.CreateItem
		(newRemoteAppointment, customProperty)
        Dim key As String = multiStatus.Response(0).HRef

        'Add the current item to the cache
        Dim newCacheItem As New CalendarCacheItem()
        newCacheItem.SyncKey = key
        newCacheItem.ExpirationDate = newAppointment.End
        newCacheItem.CreateDate = newAppointment.CreationTime
        newCacheItem.Subject = newAppointment.Subject
        newCacheItem.StartTime = newAppointment.Start

        'Add the item to the cache and save it
        Me._itemCache.Add(newAppointment.EntryID, new CacheItem)
        Me.SaveItemCache()
End Sub

Listing 4: Handling changes to existing calendar items

Private Sub ItemChangeHandler(ByVal item As Object)
        If TypeOf (item) Is Outlook.AppointmentItem Then
            Dim changedAppointment As Outlook.AppointmentItem =
			CType(item, Outlook.AppointmentItem)

            If CheckItemFilter(changedAppointment) Then

                Log.Write("Changing an appointment: " + changedAppointment.Subject)

                'Check to see if the changed item is in the cache
         If Me._itemCache.ContainsKey(changedAppointment.EntryID) Then
                    Dim currentCacheItem As CalendarCacheItem 
					= CType(Me._itemCache(changedAppointment.EntryID), CalendarCacheItem)
                    'Get the item
                    Try
                        Dim remoteAppointment As Appointment=Me._app.ExchangeResource.GetAppointment
						(currentCacheItem.SyncKey)

                        Me.CopyAttributes(changedAppointment,remoteAppointment)

                        Dim saveResult As Boolean = Me._app.ExchangeResource.SaveItem(remote-Appointment)

                    Catch ex As Exception
                        'Remove the item from the cache
                        Me._itemCache.Remove(changedAppointment.EntryID)
                        'Treat it as an add
                        Me.AddNewItem(changedAppointment)
                    End Try

                    'Update cache item
                    currentCacheItem.ExpirationDate = changedAppointment.End

                Else
                    'Treat it as a new item

                    'jbrunning:  Why treat it as new if it isn't in the cache?
					I think this is causing duplicates to 'get put onto the target exchange server.
					Commented out for now.

                    'Me.AddNewItem(changedAppointment)
                End If
                'Write out the cache
                Me.SaveItemCache()
            End If
        End If
End Sub


Listing 5: Deleting an existing calendar item

Private Sub ItemRemoveHandler(ByVal Item As Object)
   'This is called whenever a new item it added to the DeletedItems folder

        Dim removeFlag As Boolean = False

        'Make sure we're dealing with a calendar item
        If TypeOf (Item) Is Outlook.AppointmentItem Then
            Dim changedAppointment As Outlook.AppointmentItem = CType(Item, Outlook.AppointmentItem)

            Log.Write("Deleting appointment: " + changed-Appointment.Subject)

            'Find the item in our local cache.
            Dim cacheItem As CalendarCacheItem = IsItemInCache
							(changedAppointment)
            If Not cacheItem Is Nothing Then
                Try
                    Dim remoteAppointment As Appointment =
 					  Me._app.ExchangeResource.GetAppointment(cacheItem.SyncKey)
                    If CheckItemFilter(changedAppointment) Then
                        removeFlag = True
                        Me._app.ExchangeResource.Delete(remoteAppointment.Address)
                    End If

                Catch ex As Exception
                    MessageBox.Show(ex.Message)

                Finally
                    'Remove the item from the cache
                    If removeFlag Then
                        Me._itemCache.Remove(changedAppointment.EntryID)
                    End If

                End Try

                'Write out the cache
                Me.SaveItemCache()

            End If
        End If
End Sub


Listing 6: Setting up FullTrust programmatically in a custom installer class

Private Sub ConfigureCodeAccessSecurity()
        Dim machinePolicyLevel As PolicyLevel = GetPolicyLevel()
        If (GetCodeGroup(machinePolicyLevel) Is Nothing) Then
            ' Create a new FullTrust permission set
            Dim permissionSet As PermissionSet = New NamedPermissionSet(Me.namedPermissionSet)
            Dim membershipCondition As IMembershipCondition = New UrlMembershipCondition(InstallDirectory)

            ' Create the code group
            Dim policyStatement As PolicyStatement = New PolicyStatement(permissionSet)
            Dim codeGroup As CodeGroup = New UnionCodeGroup(membershipCondition, policyStatement)
            codeGroup.Description = Me.codeGroupDescription
            codeGroup.Name = Me.CodeGroupName

            ' Add the code group
            machinePolicyLevel.RootCodeGroup.AddChild(codeGroup)

            ' Save changes
            SecurityManager.SavePolicy()
        End If
End Sub