I'm building a program that fetches calendar info from multiple users in AAD. I would like to do this as efficiently as possible, so I started looking into the Microsoft graph batching functionality. I'm able to successfully do a batching query, but I'm having issues to serialize the results:
//1. construct a Batch request
var batchRequestContent = new BatchRequestContent();
var step = 1;
foreach (var userEmail in userEmails)
{
var requestUrl = graphServiceClient
.Users[userEmail]
.Calendar.Events
.Request(new List<QueryOption>
{
new QueryOption("startDateTime", start.ToString("o")),
new QueryOption("endDateTime", end.ToString("o"))
});
var request = new HttpRequestMessage(HttpMethod.Get, requestUrl.RequestUrl);
var requestStep = new BatchRequestStep(step.ToString(), request);
batchRequestContent.AddBatchRequestStep(requestStep);
step++;
}
//2. Submit request
var batchRequest = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/$batch")
{
Content = batchRequestContent
};
await graphServiceClient.AuthenticationProvider.AuthenticateRequestAsync(batchRequest);
var httpClient = new HttpClient();
var batchResponse = await httpClient.SendAsync(batchRequest);
//3. Process response
var batchResponseContent = new BatchResponseContent(batchResponse);
var responses = await batchResponseContent.GetResponsesAsync();
var responseHandler = new ResponseHandler(graphServiceClient.HttpProvider.Serializer);
foreach (var response in responses)
{
if (response.Value.IsSuccessStatusCode)
{
var responsestring = await response.Value.Content.ReadAsStringAsync();
var responseEvent = //?
}
}
Above all works, but how do I serialize this result to a strongly typed list of Events?
EDIT I tried deserializing with the ResponseHandler like this:
var batchResponseContent = new BatchResponseContent(batchResponse);
var responses = await batchResponseContent.GetResponsesAsync();
var responseHandler = new ResponseHandler(new Serializer());
foreach (var response in responses)
{
if (response.Value.IsSuccessStatusCode)
{
var events = responseHandler.HandleResponse<ICalendarEventsCollectionPage>(response.Value);
//...
}
}
But this errors out and throws the following exception:
Newtonsoft.Json.JsonSerializationException: Cannot populate JSON object onto type 'Microsoft.Graph.CalendarEventsCollectionPage'. Path '['@odata.context']', line 2, position 19.
It seems the @odata.context is responsible for the error, see image below for the actual response I get from above request: