在 Exchange Web 服务中按时间段获取约会



我有一段遗留代码,可以从日历中获取所有约会。

 private List<string> GetCalendarItemsByDay(string folderId)
    {
        var items = new List<string>();
        using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(_calenderSettings.Username, _calenderSettings.Password) })
        using (var client = new HttpClient(handler) { Timeout = TimeSpan.FromMinutes(10) })
        {
            var soapRequest =
                string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
                                   xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
                      <soap:Header>
                        <t:RequestServerVersion Version=""Exchange2010"" />
                      </soap:Header>
                      <soap:Body>
                        <FindItem xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages"" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"" Traversal=""Shallow"">
                          <ItemShape>
                            <t:BaseShape>IdOnly</t:BaseShape>
                               </ItemShape>
                               <ParentFolderIds>
                                 <t:FolderId Id=""{0}"" />
                                </ParentFolderIds>
                              </FindItem>
                            </soap:Body>
                           </soap:Envelope>", folderId);
            try
            {
                var content = new StringContent(soapRequest, Encoding.UTF8, "text/xml");
                var request = new HttpRequestMessage(HttpMethod.Post, _calenderSettings.EwsUri)
                {
                    Content = content
                };
                var response = client.SendAsync(request).Result;
                using (var responseStream = response.Content.ReadAsStreamAsync().Result)
                {
                    var nav = new XPathDocument(responseStream).CreateNavigator();
                    var nsManager = new XmlNamespaceManager(nav.NameTable);
                    nsManager.AddNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");
                    var folderNodes = nav.Select("//t:ItemId", nsManager);
                    foreach (XPathNavigator folderNode in folderNodes)
                    {
                        items.Add(folderNode.TryGetNodeValue<string>("@Id", nsManager));
                    }
                }
            }
            catch (AggregateException a)
            {
                Log.For(this).Debug(a.Message);
            }
        }
        return items;
    }

这很好用,但我只需要接下来 10 天的约会。是否可以向 SOAP 请求添加日期过滤器?

之后我尝试过滤掉它们,但响应不包含日期。

您需要在代码中使用日历视图,在其中指定 StartDate 和 EndDate。

例如

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
       xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2013_SP1" />
  </soap:Header>
  <soap:Body>
    <m:FindItem Traversal="Shallow">
      <m:ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="item:Subject" />
          <t:FieldURI FieldURI="calendar:Start" />
          <t:FieldURI FieldURI="calendar:End" />
        </t:AdditionalProperties>
      </m:ItemShape>
      <m:CalendarView MaxEntriesReturned="5" StartDate="2013-08-21T17:30:24.127Z" EndDate="2013-09-20T17:30:24.127Z" />
      <m:ParentFolderIds>
        <t:FolderId Id="AAMk" ChangeKey="AgAA" />
      </m:ParentFolderIds>
    </m:FindItem>
  </soap:Body>
</soap:Envelope>

最新更新