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.


http://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.


http://blogs.msdn.com/webdav_101/archive/2009/01/10/sample-how-to-get-the-number-of-file-attachments-with-ews.aspx


 


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 (10)

  1. aravindhanv says:

    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

    Aravind

  2. aravindhanv says:

    For the above query, I will be using Exchange 2003 only. So would prefer a webdav based approach

  3. Webdav101 says:

    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.

  4. Webdav101 says:

    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,

    Dan

  5. tim@gilbertsongroup.com says:

    thanks for the article…i have a question regarding the Sensitivity – using EWS against an Exchange 2007 box (sp2).

    Within Outlook (mixture of 2003 and 2007) the "private" flag is being set; however, when i read the calendar events – specifically the Item type, the sensitivity is always being returned as "normal."  the sensitivitySpecified is always false.

    how do i go about retrieving the sensitity for appointments within the calendar?

    thanks for the help.

  6. tim@gilbertsongroup.com says:

    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.

  7. Shojib_ says:

    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.

  8. Webdav101 says:

    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!

  9. Shojib says:

    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.

Skip to main content