Search code examples
vb.netexchangewebservicesappointment

How to get appointment with EWS by uniqueId


I create appointment using EWS without problem. I save the uniqueId like this

Dim rdvEncours As DevisRdv = GetRdv(ConnectedUser,LesDatas) 
Appointment.Save(New FolderId(WellKnownFolderName.Calendar, rdvEncours.Collaborateur.Mail))
rdvEncours.ExchangeId = Appointment.Id.UniqueId
LesDatas.SaveChange();

After I want to delete it. I've a working function based on Title,Day,hour but It's not completly safe. Then I want use my UniqueId Then I create this function

Public Function FindAppointment(Service As ExchangeService, UnikId As String) As Appointment
    Dim CalendarFolder As CalendarFolder = CalendarFolder.Bind(Service, New FolderId(WellKnownFolderName.Calendar, ml), New PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount))

    ' Set the number of items to the smaller of the number of items in the Contacts folder Or 1000.
    Dim numItems As Integer = If(CalendarFolder.TotalCount < 1000, CalendarFolder.TotalCount, 1000)

    ' Instantiate the item view with the number of items to retrieve from the contacts folder.
    ' To keep the request smaller, send only the display name.
    Dim View As ItemView = New ItemView(numItems) With {.PropertySet = New PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.End, AppointmentSchema.Start)}

    ' Create a searchfilter to check the subject of the tasks.
    Dim searchFilter As SearchFilter.SearchFilterCollection = New SearchFilter.SearchFilterCollection From {New SearchFilter.IsEqualTo(ItemSchema.Id, UnikId)}

    ' Retrieve the items in the Calendar folder with the properties you selected.
    Dim taskItems = Service.FindItems(New FolderId(WellKnownFolderName.Calendar, ml), searchFilter, View)

    If taskItems.Count = 1 AndAlso TypeOf taskItems.Items(0) Is Appointment Then
        Return taskItems.Items(0)
        HelperJournal.WriteEntry("Find Rdv by id") 'TODO:A mettre ne commentaire quand vérifier
    Else
        Return Nothing
    End If

End Function

I call it like this

Dim FoundTask As Appointment = FindAppointment(ConnectToExchange(), rdvEncours.ExchangeId)
If (FoundTask IsNot Nothing) Then FoundTask.Delete(DeleteMode.HardDelete)

But an error occured on FindAppointment

Message:La valeur spécifiée est non valide pour la propriété. Exception:Microsoft.Exchange.WebServices.Data.ServiceResponseException: La valeur spécifiée est non valide pour la propriété. à Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary() à Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary() à Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute() à Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems[TItem](IEnumerable1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) à Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ViewBase view) à Maximus.HelperAgenda.FindAppointment(ExchangeService Service, String UnikId) dans XXX\HelperAgenda.vb:ligne 50 à Maximus.VisuDevis.PoseInter(SetDevisRDV MesDonnees) dans XXX\VisuDevis.aspx.vb:ligne 560 Info Supplémentaire :Suppression RDV dans calendrier


Solution

  • The Glen comments of 4/6/2020 is the right response Here the summary

     Dim Appointment As New Appointment(service) With {
         .Subject = "MySubject",
         .Start = rdvEncours.DteInter,
         .Body = "MyBody",
         .Importance = Importance.High
     }
     Appointment.End = Appointment.Start.AddHours(1)
     Dim MaClef As String = getRandomString(256)
     Appointment.SetExtendedProperty(GetCustomKeyAppointment, MaClef)
    
     Appointment.Save(New FolderId(WellKnownFolderName.Calendar, rdvEncours.Collaborateur.Mail))
     rdvEncours.ExchangeId = MaClef
     LesDatas.SaveChanges()
    

    And, like u need to have one reference for your new custom key, in a module I've

     ' Get the GUID for the property set.
    Private Const MyCustomKeySetId As String = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    
    Public Function GetCustomKeyAppointment() As ExtendedPropertyDefinition
        Return New ExtendedPropertyDefinition(New Guid(MyCustomKeySetId), "Mykey", MapiPropertyType.String)
    End Function
    

    And To find your appointment

     Public Function FindAppointment(Service As ExchangeService, UnikId As String) As Appointment
        Dim CalendarFolder As CalendarFolder = CalendarFolder.Bind(Service, New FolderId(WellKnownFolderName.Calendar, ml), New PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount))
    
        ' Set the number of items to the smaller of the number of items in the Contacts folder Or 1000.
        Dim numItems As Integer = If(CalendarFolder.TotalCount < 1000, CalendarFolder.TotalCount, 1000)
    
        ' Instantiate the item view with the number of items to retrieve from the contacts folder.
        ' To keep the request smaller, send only the display name.
        Dim View As ItemView = New ItemView(numItems) With {.PropertySet = New PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.End, AppointmentSchema.Start)}
    
        ' Create a searchfilter to check the subject of the tasks.
        Dim searchFilter As SearchFilter = New SearchFilter.IsEqualTo(GetCustomKeyAppointment, UnikId)
    
        ' Retrieve the items in the Calendar folder with the properties you selected.
        Dim taskItems = Service.FindItems(New FolderId(WellKnownFolderName.Calendar, ml), searchFilter, View)
    
        If taskItems.Count = 1 AndAlso TypeOf taskItems.Items(0) Is Appointment Then
            Return taskItems.Items(0)
    
        Else
            Return Nothing
        End If
    
    End Function