Wednesday, 28 December 2016

Create all day event using Microsoft Graph Api

I have just developed a small application to import holiday list from csv file into every Office 365 users Outlook calendar. I have already created an Azure AD Application and set the required permission "Read and write calendars in all mailboxes". I am using following graph api endpoint to post my new calendar event.
https://graph.microsoft.com/v1.0/me/events
Json Body to add new Christmas Holiday event (2016-12-25):
{  
  "originalStartTimeZone": "UTC",
  "originalEndTimeZone": "UTC",
   "Subject":"Christmas Holiday",
   "Start":{  
      "DateTime":"2016-12-25T00:00:00",
      "TimeZone":"UTC"
   },
   "End":{  
      "DateTime":"2016-12-26T00:00:00",
      "TimeZone":"UTC"
   },
"iCalUId": "iCalUId-value",
"IsAllDay":true,
} 
To a create holiday event, we need to make sure that the event should occur in whole day (12 AM to 12 PM), to achieve this we need to set the parameter isAllDay=true and we have to set the start and end properties of the event to midnight of the day when event occurs (2016-12-25T00:00:00) and midnight of the next day of the event (2016-12-26T00:00:00).

If you set the event without giving the parameter "IsAllDay":true, the event may spread over two days due to time zone problem. So, don't forgot to set the property IsAllDay as true to create all day event like holiday.

Html Javascript Code : Create Holiday Calendar Event

var jsonBody = '{  
  "originalStartTimeZone": "UTC",
  "originalEndTimeZone": "UTC",
   "Subject":"Christmas Holiday",
   "Start":{  
      "DateTime":"2016-12-25T00:00:00",
      "TimeZone":"UTC"
   },
   "End":{  
      "DateTime":"2016-12-26T00:00:00",
      "TimeZone":"UTC"
   },
"iCalUId": "iCalUId-value",
"IsAllDay":true,
}'


var httpRequest = new XMLHttphttpRequestuest();
httpRequest.open("POST", "https://graph.microsoft.com/v1.0/me/events");
httpRequest.sethttpRequestuestHeader("authorization", "Bearer " + accessToken);
httpRequest.sethttpRequestuestHeader("content-type", "application/json");
httpRequest.onload = function (e) {
    if (httpRequest.readyState === 4) {
        if (httpRequest.status === 201) {
            console.log(httpRequest.response);
        }
    }
};
httpRequest.onerror = function (e) {
    // Handle error
};
httpRequest.send(jsonBody);

Advertisements
Advertisements

No comments:

Post a Comment