Wednesday, December 16, 2009

Update Calendar Item using Exchange web services

Hi,

Here is a sample code which is used to update a calendar item with a particular entry id and change key. For each of the property we update, we need to set the URI pointing to that property in calendar item and the object with the updated property.

private boolean sendMeetingUpdate(String id, String key)
{
boolean errorOccurred = false;
try
{
//ItemChangeType calendarChange

UpdateItemType updateCalendarItem = UpdateItemType.Factory.newInstance();

updateCalendarItem.setSendMeetingInvitationsOrCancellations(CalendarItemUpdateOperationType.SEND_TO_ALL_AND_SAVE_COPY);
updateCalendarItem.setConflictResolution(ConflictResolutionType.ALWAYS_OVERWRITE);
updateCalendarItem.setMessageDisposition(MessageDispositionType.SAVE_ONLY);

//setting the item identifier
ItemIdType itemIdentifier = ItemIdType.Factory.newInstance();
itemIdentifier.setId(id);
itemIdentifier.setChangeKey(key);

ItemChangeType[] itemChangesArray = new ItemChangeType[1];
itemChangesArray[0] = ItemChangeType.Factory.newInstance();
itemChangesArray[0].setItemId(itemIdentifier);

//Object to hold the properties.
SetItemFieldType[] itemFieldChangesArray = new SetItemFieldType[1];

//Setting the path to subject

//Forming the update calendar Object
CalendarItemType calendarItemObj = CalendarItemType.Factory.newInstance();
calendarItemObj.setSubject("Update Subject");

itemFieldChangesArray[0] = SetItemFieldType.Factory.newInstance();
itemFieldChangesArray[0].setCalendarItem(calendarItemObj);

PathToUnindexedFieldType pathToSubjectObj = PathToUnindexedFieldType.Factory.newInstance();
pathToSubjectObj.setFieldURI(UnindexedFieldURIType.ITEM_SUBJECT);

FieldURIDocument fieldURIDoc = FieldURIDocument.Factory.newInstance();
fieldURIDoc.setFieldURI(pathToSubjectObj);

itemFieldChangesArray[0].setPath(pathToSubjectObj);

NonEmptyArrayOfItemChangeDescriptionsType nonEmptyArrayOfItemChangeDescriptionsTypeObj = NonEmptyArrayOfItemChangeDescriptionsType.Factory.newInstance();
nonEmptyArrayOfItemChangeDescriptionsTypeObj.setSetItemFieldArray(itemFieldChangesArray);
itemChangesArray[0].setUpdates(nonEmptyArrayOfItemChangeDescriptionsTypeObj);

NonEmptyArrayOfItemChangesType nonEmptyArrayOfItemChangesTypeObj = NonEmptyArrayOfItemChangesType.Factory.newInstance();
nonEmptyArrayOfItemChangesTypeObj.setItemChangeArray(itemChangesArray);
updateCalendarItem.setItemChanges(nonEmptyArrayOfItemChangesTypeObj);

UpdateItemDocument updateMeetingRequest = UpdateItemDocument.Factory.newInstance();
updateMeetingRequest.setUpdateItem(updateCalendarItem);

UpdateItemResponseDocument updateItemResponseDocument = null;
System.out.println("Soap Request = \n" + updateMeetingRequest.toString());

try
{
updateItemResponseDocument = exService.updateItem(updateMeetingRequest, null, null, null, getServerVersionDetails(), null);
}
catch (Exception e)
{
System.out.println("\nError while updating calendar=" + e.getMessage());
e.printStackTrace();
return false;
}

ArrayOfResponseMessagesType arrayOfResponseMessagesType = updateItemResponseDocument.getUpdateItemResponse().getResponseMessages();
ItemInfoResponseMessageType[] infoResponseMessageType = arrayOfResponseMessagesType.getUpdateItemResponseMessageArray();

for (ItemInfoResponseMessageType type : infoResponseMessageType)
{
System.out.println("\nSOAP Response xml=" + type.xmlText());
if (type.getResponseClass() == ResponseClassType.SUCCESS)
{
ItemIdType itemId = type.getItems().getCalendarItemArray(0).getItemId();
System.out.println(itemId.getId());
System.out.println(itemId.getChangeKey());
}
else
{
System.out.println("Error occcured while createing calendar item. response = "
+ type.getResponseClass());
errorOccurred = true;
}
}
}
catch (Exception e)
{
System.out.println("Error Occured while updating meeting request - " + "\n" + e.getMessage());
e.printStackTrace();
return false;
}

if (errorOccurred)
return false;
else
return true;
}