1
votes

My task is simple. I have a public folder (a calendar actually) that I need to pull all the meetings from between two dates.

I'm using Visual Studio 2010, ASP.Net 4.0, Microsoft Exchange Server Web Services (EWS) and Exchange Server 2007.

I've been successfull in creating an item in my personal calendar by using the following code:

public static string[] CreateAppointment() 
{
    // Set up the binding with credentials and URL.
    com.webmail.ExchangeServiceBinding binding = new com.webmail.ExchangeServiceBinding();
    binding.Credentials = new System.Net.NetworkCredential("steve.kershaw", "mypasswordhere", "domain.com");
    binding.Url = @"https://webmail.com/ews/exchange.asmx";

    // Creating new appointment item type 
    com.webmail.CalendarItemType appointment = new com.webmail.CalendarItemType();  

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

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

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

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

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

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

    // Add the destination folder to the request. 
    createItemRequest.SavedItemFolderId = new com.webmail.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 
        com.webmail.CreateItemResponseType createItemResponse = binding.CreateItem(createItemRequest);  
        if (createItemResponse.ResponseMessages.Items == null || createItemResponse.ResponseMessages.Items.Length == 0) 
        { 
            return new string[] { "ERROR" }; 
        } 
        else 
        { 
            // Get the response message. 
            com.webmail.ResponseMessageType[] rmt = createItemResponse.ResponseMessages.Items;  
            if (rmt[0].ResponseClass != com.webmail.ResponseClassType.Success) 
                return new string[] 
                { "ERROR: " + rmt[0].MessageText 
                }; 
            else 
            { 
                foreach (com.webmail.ResponseMessageType rmtItem in rmt) 
                { 
                    com.webmail.ArrayOfRealItemsType itemArray = (rmtItem as com.webmail.ItemInfoResponseMessageType).Items; 
                    com.webmail.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; 
}

However, I've been unable to find any code to successfully pull any appointment from any calendar, including my own. The (current) iteration of my code returns an "Id is malformed" error in the com.webmail.GetItemResponseType resp = binding.GetItem(getItemType); return in the ResponseMessages.Items.MessageText value. This results in a null value for the returned Items. My code follows:

public static void GetCalendarItem()
{
    // Set up the binding with credentials and URL.
    com.webmail.ExchangeServiceBinding binding = new com.webmail.ExchangeServiceBinding();
    binding.Credentials = new System.Net.NetworkCredential("steve.kershaw", "mypasswordhere", "domain.com");
    binding.Url = @"https://webmail.com/ews/exchange.asmx";

    // Get the Itemtype...
    com.webmail.GetItemType getItemType = new com.webmail.GetItemType();

    ////getItemType.
    //com.webmail.GetItemResponseType getItemResponseType = binding.GetItem(getItemType);
    // Create the response shape.
    com.webmail.ItemResponseShapeType responseShape = new com.webmail.ItemResponseShapeType();
    responseShape.BodyType = com.webmail.BodyTypeResponseType.Text;
    responseShape.BodyTypeSpecified = true;
    responseShape.BaseShape = com.webmail.DefaultShapeNamesType.Default;
    // Add more properties to the request.
    com.webmail.PathToUnindexedFieldType[] sensitivity = new com.webmail.PathToUnindexedFieldType[1];
    sensitivity[0] = new com.webmail.PathToUnindexedFieldType();
    sensitivity[0].FieldURI = com.webmail.UnindexedFieldURIType.itemSensitivity;
    responseShape.AdditionalProperties = sensitivity;
    // Add the response shape to the request.
    getItemType.ItemShape = responseShape;

    // Identify the items to get.
    com.webmail.ItemIdType[] items = new com.webmail.ItemIdType[2];
    items[0] = new com.webmail.ItemIdType();
    items[0].Id = "AAAlAE1BQG1";
    items[0].ChangeKey = "DwAAABYAAAA";
    items[1] = new com.webmail.ItemIdType();
    items[1].Id = "AAAlAE1BQG1";
    items[1].ChangeKey = "DwAAABYAAAA";

    // Add items to the request.
    getItemType.ItemIds = items;

    try
    {
        // Send the request and get the response.
        com.webmail.GetItemResponseType resp = binding.GetItem(getItemType);
        com.webmail.ArrayOfResponseMessagesType aormt = resp.ResponseMessages;
        com.webmail.ResponseMessageType[] rmta = aormt.Items;

        foreach (com.webmail.ResponseMessageType rmt in rmta)
        {
            com.webmail.ItemInfoResponseMessageType iirmt = (rmt as com.webmail.ItemInfoResponseMessageType);
            com.webmail.ArrayOfRealItemsType aorit = iirmt.Items;
            com.webmail.ItemType[] myItems = aorit.Items;

            // Determine the type for each item and cast to the approriate type.
            foreach (com.webmail.ItemType it in myItems)
            {
                // Check whether it is an e-mail.
                if (it is com.webmail.MessageType)
                {
                    com.webmail.MessageType message = (it as com.webmail.MessageType);
                }
                // Determine whether it is a calendar item.
                else if (it is com.webmail.CalendarItemType)
                {
                    com.webmail.CalendarItemType calendar = (it as com.webmail.CalendarItemType);
                }
                else
                {
                    // Check for other item types.
                }
            }
        }
    }
    catch (Exception e)
    {
        throw new Exception("GetItem failed");
    }
}

Is there an easy way to just pull ALL the appointments in a calendar for a certain date range?!

1
I guess nobody has an answer!!! :-( - stevekershaw
Your ItemId and ChangeKey look horribly wrong. They should be of the form <typ:ItemId Id="AQMkADkyZTQxNjUzLTcwZTQtNGRlNS04M2VmLWMxYmIBNWJiADUwZTYARgAAA4Kt4mOTlXZJrZx0v5cQm8IHAISmF1hx/2pAhQBTVUBmYgoAAAMhAAAAhKYXWHH/akCFAFNVQGZiCgACMstDjAAAAA==" ChangeKey="DwAAABYAAACEphdYcf9qQIUAU1VAZmIKAAIyy5CE"/> CreateItem should've returned you these IDs. - Jan Doggen

1 Answers

1
votes

Steve, where did you get the ID from that you put into GetItem? The usual route is to call FindItem with the mailbox smtp and from/to date. You can get most fields with FindItem, but e.g. not the body text, so then you call GetItem with one of the ID's that you got back from FindItem.

Note that FindItem will also return all occurrences of repeating events, you may want to get the master event from those first. See my answer at Delete recurring calendar item with PHP EWS?

I can't give you any C code, I'm doing it all in Delphi through my 'own' SOAP calls.