Reputation: 28
I have a piece of legacy code that gets all appointments from a calender.
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;
}
This works fine , but I only need the appointments for the next 10 days. Is it possible to add a date filter to the soap request?
I tried filtering them out afterwards, but the response does not contain dates.
Upvotes: 0
Views: 31
Reputation: 22032
You need to use a CalendarView in your code where you specify the StartDate and EndDate. eg
<?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>
Upvotes: 1