Second part of the Exchange tutorial. Last time I explained how to create ExchangeServiceBinding object  and how to add a reference to the Exchange Web Services(EWS).

Today I’ll show You how to add appointment to an Exchange calendar. Everything is explained step by step in the comments in the code below.

Enjoy!

public string[] CreateAppointment()
 {
 // Creatting new appointment item type
 CalendarItemType appointment = new CalendarItemType();

 // Add properties to the newly created appointment.
 appointment.Importance = ImportanceChoicesType.Normal;
 appointment.ImportanceSpecified = true;
 appointment.ItemClass = "IPM.Appointment";
 appointment.Subject = "mySubject";
 appointment.Body = new BodyType();
 appointment.Body.BodyType1 = BodyTypeType.HTML;
 appointment.Body.Value = "<b>Body</b>";
appointment.Categories = new string[] { "Category Red", "Category Blue" };
 appointment.Start = DateTime.Now;
 appointment.StartSpecified = true;
 appointment.End = DateTime.Now.AddDays(2);
 appointment.EndSpecified = true;
 appointment.IsAllDayEvent = false;
 appointment.IsAllDayEventSpecified = true;
 appointment.Location = "myOffice";
 appointment.LegacyFreeBusyStatus = LegacyFreeBusyType.Busy;
 appointment.LegacyFreeBusyStatusSpecified = true;
 appointment.ReminderIsSet = true;
 appointment.ReminderIsSetSpecified = true;
 appointment.ReminderMinutesBeforeStart = "60";

 // Specify the destination folder
 DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
 folder.Id = DistinguishedFolderIdNameType.calendar;

 // Create the NonEmptyArrayOfAllItemsType array that will contain the appointment.
 NonEmptyArrayOfAllItemsType arrayOfItems = new NonEmptyArrayOfAllItemsType();
 arrayOfItems.Items = new ItemType[1];

 // Add our appointment to the array.
 arrayOfItems.Items[0] = appointment;

 // Create the request.
 CreateItemType createItemRequest = new CreateItemType();

 // Set the required SendMeetingInvitations attribute
 createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
 createItemRequest.SendMeetingInvitationsSpecified = true;

 // Add the destination folder to the request.
 createItemRequest.SavedItemFolderId = new TargetFolderIdType();
 createItemRequest.SavedItemFolderId.Item = folder;

 // Add the items to the CreateItem request.
 createItemRequest.Items = arrayOfItems;

 // Return value containg changeKey and hash Id to identify our appointment(needed for deleteing etc.)
 string[] changeKeyHashId = new string[2];

 try
 {
 // Send the request - esb is a ExchangeServiceBinding object instance created in the Part 1 of this tutorial
 CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);

 if (createItemResponse.ResponseMessages.Items == null || createItemResponse.ResponseMessages.Items.Length == 0)
 {
 return new string[] { "ERROR" };
 }
 else
 {
 // Get the response message.
 ResponseMessageType[] rmt = createItemResponse.ResponseMessages.Items;

 if (rmt[0].ResponseClass != ResponseClassType.Success)
 return new string[] { "ERROR: " + rmt[0].MessageText };
 else
 {
 foreach (ResponseMessageType rmtItem in rmt)
 {
 ArrayOfRealItemsType itemArray = (rmtItem as ItemInfoResponseMessageType).Items;
 ItemType[] items = itemArray.Items;

 // Get the return values
 changeKeyHashId[0] = items[0].ItemId.ChangeKey;
 changeKeyHashId[1] = items[0].ItemId.Id;
}
 }
 }
 }
 catch (Exception ex)
 {
 return new string[] { "ERROR: " + ex.Message };
 }

 return changeKeyHashId;
 }
//----------