Reputation: 41
Given the response on batching given in this StackOverflow question at Can I add multiple events to a calendar?
It's not clear how to use the C# SDK to and pass parameters into the requests. The examples I see are about retrieving data via batch, but I don't see a clear way to pass parameters in.
My previous code (before patch), I would just call AddAsync or UpdateAsync on the Request() method. But with batch, I can't have those Add/Update methods. So this was my code:
await _graphClient.Me.Calendars[_calendarID].Events
.Request()
.AddAsync(@event);
But I don't see a clear why to add the updated event object to the calendar. How to pass in the parameter correctly?
var batch = new BatchRequestContent();
// The response given is a GET METHOD
var request = _graphClient.Me.Calendars[_calendarID].Events
.Request()
.GetHttpRequestMessage();
// UPDATE the request to POST along with data in content
// (Am I wrong here in doing it this way?)
request.Method = HttpMethod.Post;
request.Content = new StringContent(JsonConvert.SerializeObject(@event));
batch.AddBatchRequestStep(request);
var response = await _graphClient.Batch.Request().PostAsync(batch);
The above code also doesn't like the JSON that it's given into the request.Content property. Please do let me know what is the correct way to supply data to the request, and if I'm completely wrong on writing a post. Thank you!
Upvotes: 1
Views: 1351
Reputation: 41
OK, solved it! Realized that I had to change the HttpMethod to Patch but there was no enumeration rather you had to explicitly set it with a string.
var batch = new BatchRequestContent();
var serializer = new Microsoft.Graph.Serializer();
Code to ADD an Event though a batch:
var request = this.GraphClient.Me.Calendars[_calendarID].Events
.Request()
.GetHttpRequestMessage();
request.Method = HttpMethod.Post;
request.Content = serializer.SerializeAsJsonContent(outlookEvent);
batch.AddBatchRequestStep(request);
Code to UPDATE an Event through a batch:
var request = this.GraphClient.Me.Calendars[_calendarID].Events[outlookEvent.Id]
.Request()
.GetHttpRequestMessage();
request.Method = new HttpMethod("PATCH");
request.Content = serializer.SerializeAsJsonContent(outlookEvent);
batch.AddBatchRequestStep(request);
Once you've added all your adds and updates, you can just call
await this.GraphClient.Batch.Request().PostAsync(batch);
Upvotes: 3
Reputation: 142222
You are mostly on the right path.
Instead of using the direct JsonConvert.SerializeObject, you should create an instance of the Microsoft.Graph.Serializer class and use that to do the serialization. It is configured to create JSON objects that are compatible with Microsoft Graph.
Upvotes: 0