Calendar 从所有Outlook日历获取约会

Calendar 从所有Outlook日历获取约会,calendar,exchangewebservices,appointment,Calendar,Exchangewebservices,Appointment,我正在尝试使用ExchangeServiceBinding从Outlook日历读取约会,但我的解决方案仅从“默认”Outlook日历读取约会,而不从“子日历/自定义日历”读取约会。您知道如何定义其余的日历吗?或者您知道包含所有日历的更好的解决方案吗 关键的一点是,解决方案不应该包含MAPI,因为它将在web服务中再次使用 我当前的代码: private static List<List<string>> ReadCalendarEvents(string email)

我正在尝试使用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;
    }
私有静态列表ReadCalendarEvents(字符串电子邮件)
{
List calendarEvents=新列表();
//指定请求版本。
esb.RequestServerVersionValue=新的RequestServerVersion();
esb.RequestServerVersionValue.Version=ExchangeVersionType.Exchange2007;
//形成FindItem请求。
FindItemType findItemRequest=新的FindItemType();
CalendarViewType calendarView=新的CalendarViewType();
calendarView.StartDate=DateTime.Now.AddDays(-7);
calendarView.EndDate=DateTime.Now.AddDays(200);
calendarView.MaxEntriesReturned=1000;
calendarView.MaxEntriesReturnedSpecified=true;
findItemRequest.Item=calendarView;
//定义响应中返回的项属性。
ItemResponseShapeType itemProperties=新的ItemResponseShapeType();
//使用响应的默认形状。
//itemProperties.BaseShape=DefaultShapeNameType.IdOnly;
itemProperties.BaseShape=DefaultShapeNameType.AllProperties;
findItemRequest.ItemShape=itemProperties;
DifferentiedFolderIDType[]folderIDArray=新的DifferentiedFolderIDType[1];
folderIDArray[0]=新的区分FolderIDType();
folderIDArray[0]。Id=DiscrimitedFolderIDNameType.calendar;
//
FolderDarray[0]。邮箱=新的EmailAddressType();
FolderDarray[0]。邮箱。电子邮件地址=电子邮件;
findItemRequest.ParentFolderId=FolderDarray;
//定义遍历类型。
findItemRequest.Traversal=ItemQueryTraversalType.Shallow;
尝试
{
//发送FindItem请求并获取响应。
FindItemResponseType findItemResponse=esb.FindItem(findItemRequest);
//访问响应消息。
ArrayOfResponseMessageType responseMessages=findItemResponse.responseMessages;
ResponseMessageType[]rmta=responseMessages.Items;
int folderNumber=0;
foreach(rmta中的ResponseMessageType rmt)
{
//每个搜索的文件夹一个FindItemResponseMessageType。
FindItemResponseMessageType firmt=rmt作为FindItemResponseMessageType;
if(firmt.RootFolder==null)
继续;
FindItemParentType fipt=firmt.RootFolder;
对象对象=初始项;
//FindItem包含一个项目数组。
if(obj是ArrayOfRealItemsType)
{
ArrayOfRealItemsType项目=
(obj作为ArrayOfRealItemsType);
if(items.items==null)
{
folderNumber++;
}
其他的
{
foreach(items在items.items中键入它)
{
if(它是CalendarItemType)
{
CalendarItemType cal=(CalendarItemType)它;
List ce=新列表();
ce.Add(标定位置);
添加(cal.Start.ToShortDateString()+“”+cal.Start.ToShortTimeString());
添加(cal.End.ToShortDateString()+“”+cal.End.ToShortTimeString());
行政长官增补(会计科目);
如果(cal.Organizer!=null)
{
ce.Add(cal.Organizer.Item.Name);
}
calendarEvents.Add(ce);
Console.WriteLine(cal.Subject++cal.Start.ToSortDateString()++++cal.Start.ToSortTimeString()++++cal.Location);
}
}
folderNumber++;
}
}
}
}
捕获(例外e)
{
投掷;
}
最后
{
}
返回日历事件;
}

在EWS中,您需要一次查询一个文件夹,对于非默认文件夹,您首先需要找到文件夹ID,然后才能查询文件夹中的约会(或项目)。要查找邮箱中的所有日历文件夹,您需要使用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,这将大大节省您编写所需的时间和代码量

干杯
格伦

谢谢格伦,这个解决方案看起来很有希望,但是我无法让它工作,因为有一个代理。你能为我提供更多关于EWSProxy的信息吗?最重要的是,MartinEWSProxy只是我在WebService引用中使用的前缀,我已经从示例中删除了它