Part II: Query Exchange availability with Java

Although very verbose, I’m just listing the minimum you need to run the query. If you use it you will likely need to use utility methods to modularize the code and alter the input.


ExchangeServices factory = new ExchangeServices();
ExchangeServicePortType service = factory.getExchangeServicePort();

// create request
GetUserAvailabilityRequestType request = 
    new GetUserAvailabilityRequestType();

// set timezone
SerializableTimeZoneTime standardTime = new SerializableTimeZoneTime();
standardTime.setTime("00:00:00");
standardTime.setDayOrder((short)1);
standardTime.setDayOfWeek(DayOfWeekType.SUNDAY);

SerializableTimeZone timezone = new SerializableTimeZone();
timezone.setBias(-8 * 60);	// CHANGE THIS TO YOUR TIMEZONE
timezone.setStandardTime(standardTime);
timezone.setDaylightTime(standardTime);
request.setTimeZone(timezone);

// set time window of meeting
try {
  DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
  Duration duration = new Duration();
  // CHANGE THESE DATES
  duration.setStartTime(datatypeFactory.newXMLGregorianCalendar(
    new GregorianCalendar(2009, Month.DECEMBER, 8, 7, 00)));
  duration.setEndTime(datatypeFactory.newXMLGregorianCalendar(
    new GregorianCalendar(2009, Month.DECEMBER, 8, 18, 00)));

  FreeBusyViewOptionsType options = new FreeBusyViewOptionsType();
  options.setTimeWindow(duration);
  options.getRequestedView().add("Detailed"); // retrieve subject info

  request.setFreeBusyViewOptions(options);
} catch (DatatypeConfigurationException e) {
  e.printStackTrace();
  System.exit(-1);
}

// set meeting rooms to check
EmailAddress emailAddress = new EmailAddress();
// CHANGE THIS TO YOUR ROOM
emailAddress.setAddress("MeetingRoom@my.exchange.com");

MailboxData mailbox = new MailboxData();
mailbox.setEmail(emailAddress);
mailbox.setAttendeeType(MeetingAttendeeType.REQUIRED);
ArrayOfMailboxData mailboxes = new ArrayOfMailboxData();
// ADD MULTIPLE ROOMS IF NEEDED
mailboxes.getMailboxData().add(mailbox);
request.setMailboxDataArray(mailboxes);

// create response
Holder<GetUserAvailabilityResponseType> responseHolder = 
    new Holder<GetUserAvailabilityResponseType>();

service.getUserAvailability(request, responseHolder);

List<FreeBusyResponseType> responses = 
    responseHolder.value.getFreeBusyResponseArray()
    .getFreeBusyResponse();

for (FreeBusyResponseType response: responses) {
  ArrayOfCalendarEvent events = 
    response.getFreeBusyView().getCalendarEventArray();
  if (events == null) continue;
  for (CalendarEvent event: events.getCalendarEvent()) {
    System.out.printf("%s - %s : %s\n", event.getStartTime(), 
      event.getEndTime(), event.getCalendarEventDetails().getSubject());
  }
}

This code will simply print out if there are conflicting events in the “time window” you specified for the given “rooms”. If you use people as the addresses you will be checking the availability of the people. If you don’t use “Detailed” you will just get the conflicting times. There will be one FreeBusyResponseType for each address you gave as input, in the same order. If there are no conflicts, events will be null.

See Part I for how to create the stubs, and Part III for the JFreeChart GUI.

RSS feed for comments on this post · TrackBack URL

Leave a Comment