如何从Exchange Server 2007中提取公共日历数据?

时间:2013-04-30 17:01:47

标签: c# asp.net exchange-server exchangewebservices exchange-server-2007

我的任务很简单。我有一个公共文件夹(实际上是一个日历),我需要从两个日期之间拉出所有会议。

我正在使用Visual Studio 2010,ASP.Net 4.0,Microsoft Exchange Server Web服务(EWS)和Exchange Server 2007.

我已成功使用以下代码在个人日历中创建项目:

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; 
}

但是,我一直无法找到任何代码来成功从任何日历中提取任何约会,包括我自己的约会。我的代码的(当前)迭代在com.webmail.GetItemResponseType resp = binding.GetItem(getItemType)中返回“Id is malformed”错误。返回ResponseMessages.Items.MessageText值。这会导致返回的Items为null值。 我的代码如下:

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");
    }
}

是否可以轻松地将日历中的所有约会拉到特定日期范围内?!

1 个答案:

答案 0 :(得分:1)

史蒂夫,你从哪里获得了你放入GetItem的ID? 通常的路线是使用邮箱smtp和/ to date来调用 FindItem 。您可以使用FindItem获取大多数字段,例如不是正文,所以你用GetItem中的一个ID调用GetItem。

请注意,FindItem还会返回所有重复事件,您可能希望从那些事件中获取主事件。 请参阅我在Delete recurring calendar item with PHP EWS?

的回答

我不能给你任何C代码,我在Delphi中通过我自己的'SOAP'调用它。