使用finditem()和getitems()获取EWS预约

时间:2013-09-13 21:18:03

标签: c# exchangewebservices

我正在使用finditem()和getitems()来返回calendaritemtype和loaded属性。这个电话是在用户提交预约后10秒钟完成的,我可以确认我在提交后接受了房间或资源的接受。由于我的网站流量很大,我需要在创建后立即检查预约,以便接受或拒绝房间,这样可以防止同时预订。我已经将下面的方法拼凑在一起以返回与会者的回复,因此我可以传递真或假的确认。当我运行这个方法时,它列出了很多约会,但绝不是我刚创建的约会。

请记住,我立刻从房间接受了接受,我只是无法通过calendaritemtype(约会)以编程方式获得ACCEPT。

  1. 我是否需要调整我的findItem搜索以便我可以返回我的calendaritemtype(约会)?
  2. 我需要考虑同步吗?
  3. 我在手边的物品中找到了参与者,但这不是正确的项目。

    以下是我所说的方法:

    public static ResponseInfo GetCalendarEvents(ExchangeServiceBinding esb, DateTime starttime, Reservation res, Patron pat)
    {
        var startts = new TimeSpan(0, 0, 0, 0);
        var endts = new TimeSpan(0, 23, 59, 0);
        var endtime = new DateTime();
    
        //set timespan of date to beginning of dayy
        starttime = starttime.Date + startts;
        endtime = starttime.Date + endts;
    
        // Form the FindItem request.
        var findItemRequest = new FindItemType();
        var ce = new ResponseInfo();
        var calendarView = new CalendarViewType
        {
            StartDate = starttime,
            EndDate = endtime,
            MaxEntriesReturned = 100,
            MaxEntriesReturnedSpecified = true
        };
    
        findItemRequest.Item = calendarView;
    
        // Define which item properties are returned in the response.
        var itemProperties = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.IdOnly };
    
        // Define the traversal type.
        findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
    
        // Use the Default shape for the response. 
        findItemRequest.ItemShape = itemProperties;
    
        // Identify which folders to search to find items.
        var folderIDArray = new DistinguishedFolderIdType[2];
        folderIDArray[0] = new DistinguishedFolderIdType();
        folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
    
        // Add folders to the request.
        findItemRequest.ParentFolderIds = folderIDArray;
    
        try
        {
            // Send the FindItem request and get the response.
            var findItemResponse = esb.FindItem(findItemRequest);
    
            // Next, enumerate through the Items returned by FindItem.
            foreach (FindItemResponseMessageType responseMessage in findItemResponse.ResponseMessages.Items)
            {
                if (responseMessage.ResponseClass == ResponseClassType.Success)
                {
                    var mailboxItems = (ArrayOfRealItemsType)responseMessage.RootFolder.Item;
    
                    for (int itemCount = 0; itemCount < mailboxItems.Items.Length; itemCount++)
                    {
                        var inboxItem = mailboxItems.Items[itemCount];
                        // Call GetItem on each ItemId to retrieve the 
                        // item's Body property and any AttachmentIds.
                        // Form the GetItem request.
                        var getItemRequest = new GetItemType
                        {
                            ItemShape = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.AllProperties }
                        };
    
                        // AllProperties on a GetItem
                        getItemRequest.ItemIds = new ItemIdType[1];
                        getItemRequest.ItemIds[0] = inboxItem.ItemId;
    
                        var getItemResponse = esb.GetItem(getItemRequest);
    
                        // We only passed in one ItemId to the GetItem
                        // request. Therefore, we can assume that
                        // we got at most one Item back.
                        var getItemResponseMessage = getItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
                        if (getItemResponseMessage != null)
                        {
                            if (getItemResponseMessage.ResponseClass == ResponseClassType.Success && getItemResponseMessage.Items.Items != null && getItemResponseMessage.Items.Items.Length > 0)
                            {
                                inboxItem = getItemResponseMessage.Items.Items[0];
    
                                var e = (CalendarItemType)inboxItem;
    
    
                                if (e.Start == res.Start && e.End == res.End)
                                {
                                    foreach (AttendeeType at in e.RequiredAttendees)
                                    {
                                        //attendee must accept meeting & attendee email must match email in reservation
                                        if (at.ResponseType == ResponseTypeType.Accept && at.Mailbox.EmailAddress == res.EmailAddress)
                                        {
                                            ce.Mailbox = at.Mailbox.EmailAddress;
                                            ce.ApptStart = e.Start;
                                            ce.ApptEnd = e.Start;
                                            ce.ApptResponse = ResponseTypeType.Accept;
    
                                            return ce;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                ce.Mailbox = "getItemResponseMessage.ResponseClass";
                                return ce;
                            }
                        }
                        else
                        {
                            ce.Mailbox = "getItemResponseMessage is null";
                            return ce;
                        }
                    }
                }
                else
                {
                    ce.Mailbox = "findItemResponseMessage is null";
                    return ce;
                }
            }
        }
        catch (ServiceRequestException ex)
        {
            Logger.LogStringError("****Error****: GetCalendarEvents()...ServiceRequestException= " + ex);
        }
        catch (WebException ex)
        {
            Logger.LogStringError("****Error****: GetCalendarEvents()...WebException= " + ex);
        }
        catch (Exception ex)
        {
            Logger.LogStringError("****Error****: GetCalendarEvents()...Exception= " + ex);
        }
    
        return ce;
    }
    

1 个答案:

答案 0 :(得分:3)

更新:我的物品一直在系统中,但由于这些原因,我无法找到它......

  1. Exchange以UTC格式存储时间,所以如果我离开我的窗口是一天(即上午12点 - 晚上11:59)并且我正在寻找9-11点之间的约会,那么finditem()将无法捕获预约,因为它被定义为UTC时间交换(在我的情况下,UTC时间提前5小时,因此预约存储为凌晨2-4点)。确保你的窗口足够大,以处理UTC时间的差异......

      var calendarView = new CalendarViewType
                       {
                           StartDate = DateTime.Now.AddDays(-1),
                           EndDate = DateTime.Now.AddDays(1),
                           MaxEntriesReturned = 100,
                           MaxEntriesReturnedSpecified = true
                       };    
    
  2. 确保在比较项目的属性时间时,转换时间tolocaltime()......

      if (e.Start.ToLocalTime() == res.Start && e.End.ToLocalTime() == res.End)
      {
           foreach (AttendeeType at in e.RequiredAttendees)
           {
     //attendee must accept meeting & attendee email    must match email in  reservation
               if (at.ResponseType == ResponseTypeType.Accept  && at.Mailbox.EmailAddress == res.EmailAddress)
               {
                   ce.Mailbox = at.Mailbox.EmailAddress;
                   ce.ApptStart = e.Start;
                   ce.ApptEnd = e.Start;
                   ce.ApptResponse = ResponseTypeType.Accept;
                   calevents.Add(ce);
               }
           }
       }