如何使用Java EWS API搜索iCalUid的Exchange约会?

时间:2015-06-18 01:10:16

标签: java scala exchangewebservices exchange-server-2010 ewsjavaapi

我使用Java EWS libraryiCalUid在用户的日历中搜索约会(示例iCalUid我有:040000008200E00074C5B7101A82E00800000000F66E2C0D59A9D001000000000000000010000000F7A6AACB779B00429164F39AE6DD6BB9)。这是我的Scala代码:

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
}

上面的代码抛出了这个错误:

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]

然后我尝试调整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
}

但是,现在它不会抛出异常,但它也没有找到约会。我知道约会存在是因为我强迫并​​使用这段代码找到约会:

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)
}

我做错了什么?如何在给定iCalUid的情况下搜索用户的约会?使用Java / Scala代码也没问题。

1 个答案:

答案 0 :(得分:5)

显然,会议和约会之间存在差异。替换这一行:

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

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

作品。