从所有Outlook日历中获取约会



我正尝试使用ExchangeServiceBinding从Outlook日历中读取约会,但我的解决方案只从"默认"Outlook日历中获取约会,而不从"子日历/自定义日历"中读取。你们知道如何定义其余的日历吗?或者你们知道包含所有日历的更好的解决方案吗?

关键的部分是,解决方案不应该包含MAPI,因为下次在web服务中使用。

我当前的代码:

private static List<List<string>> ReadCalendarEvents(string email)
    {
        List<List<string>> calendarEvents = new List<List<string>>();
        // Specify the request version.
        esb.RequestServerVersionValue = new RequestServerVersion();
        esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007;
        // Form the FindItem request.
        FindItemType findItemRequest = new FindItemType();
        CalendarViewType calendarView = new CalendarViewType();
        calendarView.StartDate = DateTime.Now.AddDays(-7);
        calendarView.EndDate = DateTime.Now.AddDays(200);
        calendarView.MaxEntriesReturned = 1000;
        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;
        //
        folderIDArray[0].Mailbox = new EmailAddressType();
        folderIDArray[0].Mailbox.EmailAddress = email;
        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;
                                List<string> ce = new List<string>();
                                ce.Add(cal.Location);
                                ce.Add(cal.Start.ToShortDateString() + " " + cal.Start.ToShortTimeString());
                                ce.Add(cal.End.ToShortDateString() + " " + cal.End.ToShortTimeString());
                                ce.Add(cal.Subject);
                                if (cal.Organizer != null)
                                {
                                    ce.Add(cal.Organizer.Item.Name);
                                }
                                calendarEvents.Add(ce);
                                Console.WriteLine(cal.Subject + " " + cal.Start.ToShortDateString() + " " + cal.Start.ToShortTimeString() + " " + cal.Location);
                            }
                        }
                        folderNumber++;
                    }
                }
            }
        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
        }
        return calendarEvents;
    }

在EWS中,您需要一次查询一个文件夹,对于非默认文件夹,您首先需要找到FolderId,然后才能查询文件夹中的约会(或项目)。要查找邮箱中的所有日历文件夹,您需要使用FindFolder操作并创建一个限制,以将结果限制为FolderClass为IPF的文件夹。约会,例如

                // Create the request and specify the travesal type.
        FindFolderType findFolderRequest = new FindFolderType();
        findFolderRequest.Traversal = FolderQueryTraversalType.Deep;
        // Define the properties that are returned in the response.
        FolderResponseShapeType responseShape = new FolderResponseShapeType();
        responseShape.BaseShape = DefaultShapeNamesType.Default;
        findFolderRequest.FolderShape = responseShape;
        // Identify which folders to search.
        DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
        folderIDArray[0] = new DistinguishedFolderIdType();
        folderIDArray[0].Id = DistinguishedFolderIdNameType.msgfolderroot;
        IsEqualToType iet = new IsEqualToType();
        PathToUnindexedFieldType FolderClass = new PathToUnindexedFieldType();
        FolderClass.FieldURI = UnindexedFieldURIType.folderFolderClass;
        iet.Item = FolderClass;
        FieldURIOrConstantType constantType = new FieldURIOrConstantType();
        ConstantValueType constantValueType = new ConstantValueType();
        constantValueType.Value = "IPF.Appointment";
        constantType.Item = constantValueType;
        iet.FieldURIOrConstant = constantType;
        // Add the folders to search to the request.
        RestrictionType restriction = new RestrictionType();
        restriction.Item = iet;
        findFolderRequest.Restriction = restriction;
        findFolderRequest.ParentFolderIds = folderIDArray;
        try
        {
            // Send the request and get the response.
            FindFolderResponseType findFolderResponse = esb.FindFolder(findFolderRequest);
            // Get the response messages.
            ResponseMessageType[] rmta = findFolderResponse.ResponseMessages.Items;
            foreach (ResponseMessageType rmt in rmta)
            {
                // Cast to the correct response message type.
                if (((FindFolderResponseMessageType)rmt).ResponseClass == ResponseClassType.Success) {
                    foreach (FolderType folder in ((FindFolderResponseMessageType)rmt).RootFolder.Folders) {
                        Console.WriteLine(folder.DisplayName);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

您可能还想考虑使用EWS托管API,这将大大节省您编写所需的时间和代码量

干杯Glen

最新更新