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;
Comments
Anonymous
January 09, 2009
PingBack from http://www.codedstyle.com/sample-howto-display-a-list-of-calendar-items-in-listview-using-ews/Anonymous
February 09, 2009
I am trying to create a new free/busy status for a "non-exchange" user so that outlook users can view the external user's free busy info and schedule appointments accordingly. Am not able to find much literature on how to do this. Any help / pointers will be much appreciated. Thanks Regards AravindAnonymous
February 09, 2009
For the above query, I will be using Exchange 2003 only. So would prefer a webdav based approachAnonymous
February 11, 2009
Hello aravindhanv; If those are non-exchange users and only uisng Outlook, I'm not sure how you would have their free-busy info displayed.Anonymous
February 11, 2009
Hello aravindhanv; You could use WebDAV for reading the calendar, you would use a WebDAV SEARCH which includes date ranges - this is the best way to get a list of appointments/meetings - There should be samples on my blog. Thanks, DanAnonymous
December 17, 2009
The comment has been removedAnonymous
December 17, 2009
I found out my issue (see the Sensitivity post above): i had the baseShapeProperties set to "Default" instead of "All." Thanks again for the sample.Anonymous
March 02, 2014
Hi, I am new in C# as well as Exchange, can you please help me on how I can to bind calendar appointment item to list view object in C#. FYI, I am using EWS (Exchange Server 2007 -SP1) and C# (VS Express Web). I will highly appreciate if somebody help on that.Anonymous
March 03, 2014
Hello Shojib, There are several ways to do EWS calls. EwsEditor uses the Exchange managed API. The Exchange Managed API is a lot more easy to use than Visual Studio proxies. Find EwsEditor on Codeplex and pull the full source zip file and look for the CalendarMonthView code - it shows how to bind. With the managed API you would use the Appointment Class and call its bind method and pass the service object and the id of the appointment. ewseditor.codeplex.com CalendarMonthView.cs private void EditSelectedCalendarItem() Appointment oSomeAppointment = Appointment.Bind(_CurrentService, oItemTag.Id); For calendaring you either read the appointment items (which contain the main definition of a meeting/appointment, recurrence pattern and exceptions to the recurrence pattern) as items OR would use a calendar view (which shows each instance of a meeting/appointment across a date span. For most client calendaring you would use a calendar view then use the id of a returned appointment to bind to. Calendaring is one of the most complex areas of programming, so if you get frustrated at times then don't worry - your just dealing with one of the struggles every developer has run up against. The Exchange Managed API and EWS wrap most of the complexities of calendaring - just be glad you are not having to use WebDAV or MAPI to do calendaring (they require very deep knowledge of calendaring to do many types of operations). I suggest reading the following on the Exchange managed API: msdn.microsoft.com/.../dd633710(v=exchg.80).aspx msdn.microsoft.com/.../jj220499(v=exchg.80).aspx msdn.microsoft.com/.../dd633678(v=exchg.80).aspx EwsEditor is an ongoing sample that I and others here add onto as time permits. Enjoy!Anonymous
March 04, 2014
Thank you so much for your quick reply and I appreciate. I have successfully extract all the calendar appointment using EWS and every user now can see their own appointment (Item) from my application. As my application is browser based (C#) and I need to make all appointment more interactive for my user e.g. when they will click on specific appointment then another event/ page will be generated. Could you please help me on that part? Thanks in advance.