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

2.3k views Asked by At

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.

1

There are 1 answers

1
pathikrit On BEST ANSWER

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.