Sample: Howto display a list of calendar items in ListView using EWS
Here is a helpful sample of using EWS to get a list of calendar items and displaying them in a ListView.
You should be able to use this with he sample I published prior on creating a CalendarView:
Sample: Using Calendar Views with EWS.
https://blogs.msdn.com/webdav_101/archive/2009/01/05/sample-using-calendar-views-with-ews.aspx
The sample below calls GetFileAttachmentsCount, which I have blogge here:
Sample: How to get the number of file attachments with EWS.
OK, on to the sample:
//-------------------------------------------------------------------------------------------
// RequestAndDisplayCalendarFullView
// Populates the ListView with a list of appointments defined in the calendar view.
//-------------------------------------------------------------------------------------------
public static void RequestAndDisplayCalendarFullView(
ExchangeServiceBinding binding,
CalendarViewType calendarView,
ref ListView lvView)
{
FindItemType findItemRequest = new FindItemType();
findItemRequest.ParentFolderIds = new DistinguishedFolderIdType[] { new DistinguishedFolderIdType() };
((DistinguishedFolderIdType)findItemRequest.ParentFolderIds[0]).Id =
DistinguishedFolderIdNameType.calendar;
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
ItemResponseShapeType itemShapeDefinition = new ItemResponseShapeType();
itemShapeDefinition.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.Item = calendarView;
findItemRequest.ItemShape = itemShapeDefinition;
// Do the EWS Call.
FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);
if (findItemResponse.ResponseMessages.Items[0].ResponseClass !=
ResponseClassType.Success)
{
// Indicate we have a problem
throw new Exception(String.Format(
"Unable to get calendar view\r\n{0}\r\n{1}",
findItemResponse.ResponseMessages.Items[0].ResponseCode,
findItemResponse.ResponseMessages.Items[0].MessageText));
}
FindItemResponseMessageType findItemResponseMessage =
(FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
ArrayOfRealItemsType findItemResponseItems =
(ArrayOfRealItemsType)findItemResponseMessage.RootFolder.Item;
ListViewItem lvItem = null;
int iAttachmentCount = 0;
lvView.Clear();
lvView.View = View.Details; // Set the view to show details.
lvView.GridLines = true; // Display grid lines.
lvView.Columns.Add("Subject", 200, HorizontalAlignment.Left);
lvView.Columns.Add("Recurring", 80, HorizontalAlignment.Left);
lvView.Columns.Add("Start", 120, HorizontalAlignment.Left);
lvView.Columns.Add("End", 120, HorizontalAlignment.Left);
lvView.Columns.Add("Organizer", 80, HorizontalAlignment.Left);
lvView.Columns.Add("#@", 40, HorizontalAlignment.Left);
lvView.Columns.Add("Meeting", 80, HorizontalAlignment.Left);
for (int x = 0;
x < findItemResponseMessage.RootFolder.TotalItemsInView;
x++)
{
CalendarItemType currentCalendarItem =
(CalendarItemType)findItemResponseItems.Items[x];
Debug.WriteLine(currentCalendarItem.Subject);
lvItem = new ListViewItem(String.Format("{0}", currentCalendarItem.Subject, 0));
lvItem.SubItems.Add(currentCalendarItem.IsRecurring.ToString());
lvItem.SubItems.Add(currentCalendarItem.Start.ToLocalTime().ToString());
lvItem.SubItems.Add(currentCalendarItem.End.ToLocalTime().ToString());
lvItem.SubItems.Add(String.Format("{0}", currentCalendarItem.Organizer.Item.Name));
iAttachmentCount = EwsAttachments.GetFileAttachmentsCount(binding, currentCalendarItem.ItemId);
lvItem.SubItems.Add(String.Format("{0}", iAttachmentCount.ToString()));
lvItem.SubItems.Add(String.Format("{0}", currentCalendarItem.IsMeeting.ToString()));
lvItem.Tag = string.Format("{0}:{1}", currentCalendarItem.ItemId.Id, currentCalendarItem.ItemId.ChangeKey);
lvView.Items.Add(lvItem);
lvItem = null;
}
}
Ews; Exchange Web Services; list; appointments; appointment; calendaritem; DevMsgTeam; CalendarViewType; meeting;