In my previous articles, I have written about site collection, user properties, fetching access tokens, retrieving and sending emails, and lots more. I have added the links below for your reference. Please go through these before following the steps given in this article.
Retrieve Events From The Calendar
Tested in the Graph Explorer, it returns the JSON data like below. In this article, we are going to retrieve events from Office 365 Calendar.
https://graph.microsoft.com/v1.0/users/[email protected]/events
Retrieve Calendar Events Using Microsoft Graph API
It returns all the properties from the calendar, like Startdate, EndDate, Subject, BodyContent, and Priority, etc. So, let's see how to retrieve this programmatically using JavaScript.

Please follow my previous article, How to fetch access token, to authenticate your web application to fetch the access token and authenticate.

Step 1 - Fetch Access Token

AuthUrl: https://login.microsoftonline.com/{{tenant}}/oauth2/v2.0/token

Type: POST
  1. var token; // Initialize Globally
  2. function requestToken() {
  3. $.ajax({
  4. "async": true,
  5. "crossDomain": true,
  6. "url": "https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
  7. "method": "POST",
  8. "headers": {
  9. "content-type": "application/x-www-form-urlencoded"
  10. },
  11. "data": {
  12. "grant_type": "client_credentials",
  13. "client_id ": "8baf0301-27df-44b1-b4fe-7911b9a918de", //Provide your app id
  14. "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", //Provide your secret
  15. "scope ": "https://graph.microsoft.com/.default"
  16. },
  17. success: function(response) {
  18. log(response);
  19. token = response.access_token; //Store the token into global variable
  20. },
  21. error: function(error) {
  22. log(JSON.stringify(error));
  23. }
  24. })
  25. }
The successful response is shown below.
Retrieve Calendar Events Using Microsoft Graph API
EndPoint
https://graph.microsoft.com/v1.0/users/{{userid}}/Events

Method

GET

Code

  1. function RetrieveCalendarEvents() {
  2. $.ajax({
  3. method: 'GET',
  4. url: "https://graph.microsoft.com/v1.0/users/[email protected]/events",
  5. headers: {
  6. 'Authorization': 'Bearer ' + token,
  7. 'Content-Type': 'application/json'
  8. },
  9. success: function(response) {
  10. var data = response.value;
  11. data.map(function(events) {
  12. $('#display').append('<li>StartDate: ' + events.start.dateTime + ' </br></br> EndDate: ' + events.end.dateTime + ' </br></br> Subject: ' + events.subject + '</li></br></br>');
  13. })
  14. },
  15. error: function(error) {},
  16. })
  17. }
HTTP "200", success -- it returns the JSON data Below,
Retrieve Calendar Events Using Microsoft Graph API
Then, apply .map to iterate over the JSON data and display it on the page.
Full Code
  1. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
  2. <script type="text/javascript">
  3. var token;
  4. $(document).ready(function() {
  5. requestToken();
  6. });
  7. function requestToken() {
  8. $.ajax({
  9. "async": true,
  10. "crossDomain": true,
  11. "url": "https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
  12. "method": "POST",
  13. "headers": {
  14. "content-type": "application/x-www-form-urlencoded"
  15. },
  16. "data": {
  17. "grant_type": "client_credentials",
  18. "client_id ": "8baf0301-27df-44b1-b4fe-7911b9a918de", //Provide your app id
  19. "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", //Provide your secret
  20. "scope ": "https://graph.microsoft.com/.default"
  21. },
  22. success: function(response) {
  23. console.log(response);
  24. token = response.access_token;
  25. // console.log(token);
  26. RetrieveCalendarEvents();
  27. },
  28. error: function(error) {
  29. console.log(JSON.stringify(error));
  30. }
  31. })
  32. }
  33. function RetrieveCalendarEvents() {
  34. $.ajax({
  35. method: 'GET',
  36. url: "https://graph.microsoft.com/v1.0/users/[email protected]/events",
  37. headers: {
  38. 'Authorization': 'Bearer ' + token,
  39. 'Content-Type': 'application/json'
  40. },
  41. success: function(response) {
  42. var data = response.value;
  43. console.log(response);
  44. data.map(function(events) {
  45. $('#display').append('<li>StartDate: ' + events.start.dateTime + ' </br></br> EndDate: ' + events.end.dateTime + ' </br></br> Subject: ' + events.subject + '</li></br></br>');
  46. })
  47. },
  48. error: function(error) {
  49. },
  50. })
  51. }

The final result is shown on the screen below. I have printed some basic infomation to HTML.

Retrieve Calendar Events Using Microsoft Graph API
So now, we are able to retrieve the events from Office 365 Calendars using Microsoft Graph API.
Happy SharePointing!....