How to search for an Exchange appointment for an iCalUid using Java EWS API?
Asked Answered
T

1

6

I am using the Java EWS library to search for appointments in a user's calendar by iCalUid (example iCalUid I have: 040000008200E00074C5B7101A82E00800000000F66E2C0D59A9D001000000000000000010000000F7A6AACB779B00429164F39AE6DD6BB9). Here is my Scala code:

import microsoft.exchange.webservices.data._

import java.net.URI
import java.util.Date

val exchange = {
  val service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
  service.setUrl(new URI(host))
  service.setCredentials(new WebCredentials(user, pass))
  service
}

def calendarFolderFor(email: String) = new FolderId(WellKnownFolderName.Calendar, Mailbox.getMailboxFromString(email))

def findMatchingAppointments(iCalUid: String, email: String) = {
  val searchFilter = new SearchFilter.IsEqualTo(AppointmentSchema.ICalUid, iCalUid)
  exchange.findItems(calendarFolderFor(email), searchFilter, new ItemView(1)).getItems
}

The above code throws this error:

microsoft.exchange.webservices.data.ServiceResponseException: The property can not be used with this type of restriction.
  at microsoft.exchange.webservices.data.ServiceResponse.internalThrowIfNecessary(ServiceResponse.java:262) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
  at microsoft.exchange.webservices.data.ServiceResponse.throwIfNecessary(ServiceResponse.java:251) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
  at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:146) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
  at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:807) ~[ews-java-api-1.3-SNAPSHOT.jar:na]
  at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:851) ~[ews-java-api-1.3-SNAPSHOT.jar:na]

I then tried adapting the code from this thread:

def findMatchingAppointments(iCalUid: String, email: String) = {
  import org.apache.commons.codec.binary.{Hex, Base64}
  val searchFilter = new SearchFilter.IsEqualTo(
    new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x03, MapiPropertyType.Binary),
    Base64.encodeBase64String(Hex.decodeHex(iCalUid.toCharArray))
  )
  val view = new ItemView(1)
  view.setPropertySet(new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.ICalUid))        
  exchange.findItems(calendarFolderFor(email), searchFilter, view).getItems
}

But, now it does not throw an exception but it does not find the appointment either. I know the appointment exists because I brute-forced and found the appointment using this snippet of code:

def bruteForceFind(start: Date, end: Date, iCalUid: String, email: String) = {
  val view = new CalendarView(start, end, 100)
  exchange.findAppointments(calendarFolderFor(email), view).getItems
  val allAppointments = exchange.findAppointments(calendarFolderFor(email), view).getItems
  allAppointments.filter(_.getICalUid == iCalUid)
}

What am I doing wrong? How can I search for appointments for a user given the iCalUid? Working Java/Scala code is okay too.

Tetrode answered 18/6, 2015 at 1:10 Comment(0)
T
5

Apparently there is a difference between a meeting and an appointment. Replacing this line:

new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 0x03, MapiPropertyType.Binary),

with

new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, 0x03, MapiPropertyType.Binary)

works.

Tetrode answered 18/6, 2015 at 20:19 Comment(1)
An appointment is a simple calendar entry which turns into a meeting as soon as you add one or more attendees. Little caveat: When you delete all Attendees from a Meeting it somtimes stays a meeting and all appointments have the creator as an inivisible attendee setOconner

© 2022 - 2024 — McMap. All rights reserved.