Reputation: 31
I'm currently trying to create events on a users Outlook Calendar using the Microsoft Graph API with ASP .NET MVC. Unfortunately the documentation for creating the events does not include samples. I've found and tried to modify the sample for sending emails ( here: https://github.com/microsoftgraph/aspnet-connect-rest-sample ). I was able to send an initial events totally blank to my calendar. When attempting to send the event object itself I am met with the status response BAD REQUEST. If anyone could help, it'd be greatly appreciated.
Upvotes: 2
Views: 515
Reputation: 649
For a reference of how you might construct the Event object, you can take a look how the official Microsoft Graph SDK has constructed this object: see the latest source here on GitHub.
For an example of making this REST call without using the SDK, you can reference UserSnippets#CreateEventAsync()
- an excerpt is pasted below. While the below example doesn't use native objects for serialization, it hopefully conveys the essence of how it might work.
HttpClient client = new HttpClient();
var token = await AuthenticationHelper.GetTokenHelperAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
// Endpoint for the current user's events
Uri eventsEndpoint = new Uri(serviceEndpoint + "me/events");
// Build contents of post body and convert to StringContent object.
// Using line breaks for readability.
// Specifying the round-trip format specifier ("o") to the DateTimeOffset.ToString() method
// so that the datetime string can be converted into an Edm.DateTimeOffset object:
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Roundtrip
string postBody = "{'Subject':'Weekly Sync'," + "'Location':{'DisplayName':'Water Cooler'}," + "'Attendees':[{'Type':'Required','EmailAddress': {'Address':'[email protected]'} }]," + "'Start': {'DateTime': '" + new DateTime(2014, 12, 1, 9, 30, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'End': {'DateTime': '" + new DateTime(2014, 12, 1, 10, 0, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'Body':{'Content': 'Status updates, blocking issues, and next steps.', 'ContentType':'Text'}}";
var createBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(eventsEndpoint, createBody);
if (response.IsSuccessStatusCode) {
string responseContent = await response.Content.ReadAsStringAsync();
jResult = JObject.Parse(responseContent);
createdEventId = (string) jResult["id"];
Debug.WriteLine("Created event: " + createdEventId);
} else {
// some appropriate error handling here
}
For an example of what the transmitted JSON might look like:
{
"subject": "Weekly Sync",
"location": {
"displayName": "Water Cooler"
},
"attendees": [
{
"type": "Required",
"emailAddress": {
"address": "[email protected]"
}
}
],
"start": {
"dateTime": "2016-02-02T17:45:00.0000000",
"timeZone": "UTC"
},
"end": {
"dateTime": "2016-02-02T18:00:00.0000000",
"timeZone": "UTC"
},
"body": {
"content": "Status updates, blocking issues,and nextsteps.",
"contentType": "Text"
}
}
Additional help: There's a helpful Graph Explorer Sandbox available for you to test requests in your browser
Upvotes: 2