Paul Speranza's .Net Life

Life with .Net

Getting calendar items using Exchange Web Services

I am no expert at exchange, let alone Exchange Web Services (EWS), but I recently had to use it to get at calendar information for a project. Let me tell you that the documentation for EWS sucks and the API is not very intuitive. That said, I was able to piece together what I needed. The code is not perfect and could definitely use a good once over by someone with more than 2 days experience.

The code will show how to get at calendar items, how to use a date range and how to get the body of the calendar item. I racked my brain for a while to get the body - you have  to make a call to get the calendar items and then for each item you have to go and use the ID to get its body in another call. The  idea is that the first call gets only the small parts of an item. If you want things like attachments then you make another call to get it. Here is a link to an article that gets into it.

The code is not perfect but I want to save someone the time that I spent. Please leave comments with suggestions and improvements.

UPDATE: I have attached some code. I don’t have access to an exchange server. It compiles but at least the classes are there for you. 

private List<CalendarInfo> GetCalendarEvents()
{
    List<CalendarInfo> calendarEvents = new List<CalendarInfo>();

    ExchangeServiceBinding esb =
        new ExchangeHelper().GetExchangeBinding("myUserName", "myPassword", "myDomain");

    // Form the FindItem request.
   
FindItemType findItemRequest = new FindItemType();

    CalendarViewType calendarView = new CalendarViewType();
    calendarView.StartDate = DateTime.Now.AddDays(-1);
    calendarView.EndDate = DateTime.Now.AddDays(1);
    calendarView.MaxEntriesReturned = 100;
    calendarView.MaxEntriesReturnedSpecified = true;

    findItemRequest.Item = calendarView;

    // Define which item properties are returned in the response.
   
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
    // Use the Default shape for the response.
    //itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
   
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
    findItemRequest.ItemShape = itemProperties;

    DistinguishedFolderIdType[] folderIDArray =
        new DistinguishedFolderIdType[1];
    folderIDArray[0] = new DistinguishedFolderIdType();
    folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;

    if (!string.IsNullOrEmpty(criteria.EmailAddress))
    {
        folderIDArray[0].Mailbox = new EmailAddressType();
        folderIDArray[0].Mailbox.EmailAddress = "myEmail.com";
    }
   
    findItemRequest.ParentFolderIds = folderIDArray;

    // Define the traversal type.
   
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

    try
   
{
        // Send the FindItem request and get the response.
       
FindItemResponseType findItemResponse =
            esb.FindItem(findItemRequest);

        // Access the response message.
       
ArrayOfResponseMessagesType responseMessages =
            findItemResponse.ResponseMessages;
        ResponseMessageType[] rmta = responseMessages.Items;

        int folderNumber = 0;

        foreach (ResponseMessageType rmt in rmta)
        {
            // One FindItemResponseMessageType per folder searched.
           
FindItemResponseMessageType firmt =
                rmt as FindItemResponseMessageType;

            if (firmt.RootFolder == null)
                continue ;

            FindItemParentType fipt = firmt.RootFolder;
            object obj = fipt.Item;

            // FindItem contains an array of items.
           
if (obj is ArrayOfRealItemsType)
            {
                ArrayOfRealItemsType items =
                    (obj as ArrayOfRealItemsType);
                if (items.Items == null)
                {
                    folderNumber++;
                }
                else
               
{
                    foreach (ItemType it in items.Items)
                    {

                        if (it is CalendarItemType)
                        {
                            CalendarItemType cal = (CalendarItemType)it;
                            CalendarInfo ce = new CalendarInfo();

                            ce.Location = cal.Location;
                            ce.StartTime = cal.Start;
                            ce.EndTime = cal.End;
                            ce.Subject = cal.Subject;
                            ce.Body = GetMeetingBody(esb, cal);

                            calendarEvents.Add(ce);
                        }

                    }

                    folderNumber++;
                }
            }
        }

    }
    catch (Exception e)
    {
        throw;
    }
    finally
   
{
       
    }

    return calendarEvents;
}

private string GetMeetingBody(ExchangeServiceBinding binding, CalendarItemType meeting)
{
    string meetingBody = string.Empty;
    CalendarItemType temp = null;

    // Call GetItem on each ItemId to retrieve the
    // item’s Body property and any AttachmentIds.
    //
    // Form the GetItem request.
   
GetItemType getItemRequest = new GetItemType();

    getItemRequest.ItemShape = new ItemResponseShapeType();
    // AllProperties on a GetItem request WILL return
    // the message body.
   
getItemRequest.ItemShape.BaseShape =
        DefaultShapeNamesType.AllProperties;

    getItemRequest.ItemIds = new ItemIdType[1];
    getItemRequest.ItemIds[0] = (BaseItemIdType)meeting.ItemId;

    // Here is the call to exchange.
   
GetItemResponseType getItemResponse =
        binding.GetItem(getItemRequest);

    // We only passed in one ItemId to the GetItem
    // request. Therefore, we can assume that
    // we got at most one Item back.
   
ItemInfoResponseMessageType getItemResponseMessage =
        getItemResponse.ResponseMessages.Items[0]
        as ItemInfoResponseMessageType;

    if (getItemResponseMessage != null)
    {
        if (getItemResponseMessage.ResponseClass ==
            ResponseClassType.Success
            && getItemResponseMessage.Items.Items != null
           
&& getItemResponseMessage.Items.Items.Length > 0)
        {
            temp = (CalendarItemType)getItemResponseMessage.Items.Items[0];

            if (temp.Body != null)
                meetingBody = temp.Body.Value;
        }
    }

    return meetingBody;
}

private ExchangeServiceBinding GetExchangeBinding(
      string userName, string passwotrd, string domain)
  {

      ExchangeServiceBinding binding = new ExchangeServiceBinding();

      ServicePointManager.ServerCertificateValidationCallback =
              delegate(Object obj, X509Certificate certificate, 
              X509Chain chain, SslPolicyErrors errors)
              {
                  // Replace this line with code to validate server certificate.
                  return true;
              };

      System.Net.WebProxy proxyObject = 
          new System.Net.WebProxy();
      proxyObject.Credentials = 
          System.Net.CredentialCache.DefaultCredentials;

      binding.Credentials = 
          new NetworkCredential(userName, password, domain);
 
      string server = ConfigurationManager.AppSettings["ExchangeServer"] as string;

      if (server == null || string.IsNullOrEmpty(server))
          throw new ArgumentNullException("The Exchange server Url could not be found.");

      binding.Url = server;
      Console.WriteLine("***** " + server);

      binding.Proxy = proxyObject;

      return binding;
  }

Comments

Glen H said:

Thanks a lot for a great sample!  For all the to-do that Microsoft makes about Web services being the official API for Exchange 2007, I am still having difficulty getting anything to work.  Your's is the first example I've seen using a webproxy object during the binding phase.  Thanks again, keep up the great posts!  Code attachments for download would be a big help also.

Glen

# March 27, 2008 12:53 PM

SBC said:

there's a company that's working on a proxy layer for the Exchange Web Services - (http://www.independentsoft.de/)

# April 13, 2008 5:31 PM

Francis Pauchet said:

Thanks a lot ! You spared me a lot of pain, sorry, of work.

# April 17, 2008 11:04 AM

SBC said:

Exchange Web Services library -

www.independentsoft.de/.../index.html

# June 16, 2008 6:02 AM

Johannes said:

thanks a lot - very helpful

# August 29, 2008 9:32 AM

M. Adams said:

Thanks for the code, this helped me get a head start on my project of pulling calendar info from Exchange 2007. One item that I wanted to point out is that datetimes are stored as GMT in Exchange 20007. So doing queries, you will need to do some conversions between GMT and local times. Otherwise you will be displaying GMT time for your appointments or meeting and querying based on local time against GMT.

# September 5, 2008 10:12 AM

Rupak Ganguly said:

Do you have any idea how to access a mailbox, read its body and attachments? I would really appreciate any help in this direction.

# September 22, 2008 4:15 PM

Lewis said:

 

 

 

Please could you provide the namespaces the following?

   CalendarItemType

   CalendarInfo

   criteria.EmailAddress

And to complete you excellent example/article would you be able to point me in the direction of the code for the following?

   ExchangeHelper()

# December 17, 2008 7:50 AM

Paul Speranza said:

@Lewis

Send me your email address and I'll give you what I have.

# December 17, 2008 3:06 PM

Ramen Das said:

Can I have the namespaces for the classes/types used in the code?

# January 14, 2009 7:15 AM

Paul Speranza said:

@ Ramen

Send me your email

# January 14, 2009 5:31 PM

Harsh said:

Hi,

This might be asking too much, but can you send me the code on my email harsh.puri@gmail.com

It would be a great help.

Thanks

Harsh

# February 6, 2009 11:09 AM

Jim Barbour said:

Hello, if anyone knows what the XML that this

code generates looks like, I'd be very

grateful to see it. I'm trying

to do this very thing in raw XML

I'd love to hear from anyone.

Jim  x38444@barcore.com

# February 15, 2009 3:45 PM

JustJae said:

You need the EWS library found here www.microsoft.com/.../details.aspx

Add a refrenece to your project and this should clear things up a bit.

i'm new to C# and would love if someone could post a working example or some working code as a console project with the existing code.

# April 23, 2009 10:51 PM

Rod said:

Can I have the namespaces for the classes/types used in the code?

# June 1, 2009 6:11 PM

Debjit Das said:

Thanks for the code. However I require a list of participants (required,optional) for a meeting. I tried getting the same using RequiredAttendees and OptionalAttendees properties of CalendarItemType but getting null value for the same although there are required and optional attendees set for the meeting in the Exchange Server. I am using the code like below

AttendeeType[] attendee_list = calItemType.RequiredAttendees;

                               AttendeeType[] attendee_list1 = calItemType.OptionalAttendees

but getting null values for attendee_list and attendee_list1

What might be wrong here ?

# June 4, 2009 1:49 AM

Deewaker said:

Nice work.

Thanks

I could not get the namespace for the CalendarInfo class. which namespace it is using??

please let me know.

Regards

Deewaker

# June 11, 2009 7:29 AM

Deewaker said:

Please tell the namespaces it is using.

My mail id is deewaker_mca@yahoo.co.in

Regards

Deewaker

# June 12, 2009 1:28 AM

Edu said:

Please could you provide the namespaces the following?

  CalendarItemType

  CalendarInfo

  criteria.EmailAddress

Mi email: e_pedrosa@hotmail.com

# June 18, 2009 3:27 AM

Rod said:

I also need namespaces the following?

 CalendarItemType

 CalendarInfo

 criteria.EmailAddress

rmerritt@rivcoeda.org

# July 2, 2009 6:10 PM

Phil Gianuzzi said:

If you have time to shoot me an email as well, I was interested in your CalendarInfo class and further explanation of criteria.EmailAddress.  The example looks great, this is the first really useful example of using ExchangeWebServices that I've seen.

phil.gianuzzi@jhu.edu

# July 14, 2009 3:29 PM

Sinh Nguyen said:

I don't know what criteria.EmailAddress does. Please explain help me. My Email is ngsinhnguyen20c2@yahoo.com. And could you guide me how to work with another objects in the Exchange, what are they namespace, library to use? Thanks a lot

# July 16, 2009 12:39 AM

Mike Kelly said:

I hate when examples use things like this:

new ExchangeHelper().GetExchangeBinding("myUserName", "myPassword", "myDomain");

Who's going to hardcode a password into code?  The real way to do this is to use a prompt or use the default credentials for the user.

# July 23, 2009 9:04 PM

Ruben said:

for credentials, use:

esb.Credentials = CredentialCache.DefaultCredentials;

# July 27, 2009 11:02 AM

Cesar said:

Hello,

I've referenced my EWS service and downloaded the Exchange Web Services Managed API Beta (was it useful?).

What do I need to reference now to get CalendarInfo ?

Thanks

My email : needhelpews@yopmail.com

# July 31, 2009 6:37 AM

Adrian said:

It would be very much appreciated if you could also give me the namespaces the following?

CalendarItemType

CalendarInfo

criteria.EmailAddress

Thanks Paul, this is very helpful

ng.adrian@gmail.com

# August 6, 2009 5:30 PM

Joe said:

Just found your post, and as some of the others have said, I am also in need of the references for:

CalendarInfo

ExchangeHelper

criteria.EmailAddress

Thanks again, great post!

synacksolutions@gmail.com

# September 14, 2009 2:53 AM

Paul Speranza said:

Thanks to all of you for putting up with me not having the code. I finally found it and had to rip out the company name that I wrote it at. You should be good from there. See the attachment.

Paul

# September 15, 2009 7:12 PM

Nathan said:

Just wanted to let people know that MS doesn't recommend working against the SOAP interface directly, but rather by using the EWS Managed API. I too struggled with the SOAP interface before finding that gem.

msdn.microsoft.com/.../dd633710.aspx

Hope that helps.

# September 22, 2009 7:09 PM

senthilkumar said:

pls send me an working example.

thanks in advance

# November 2, 2009 4:09 AM

Geovani said:

Do the Appointments or Tasks have unique ID's? For example in .NET I could pull a Unique ID by calling the GUID. I would like to pull the Appointment ID or Task ID and save it to a Database and use it as a reference on my other programs.

# November 2, 2009 9:52 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)