Accessing Office 365 / Microsoft Cloud Data On SharePoint Using Graph API - Part Two


In this article series, we are focusing on accessing the Office 365 and Microsoft cloud services data using Graph REST API calls. Here, in this article, let us see how to access the Office 365 Outlook calendar events with the help of Microsoft Graph API.

Microsoft Graph exposes Office 365 and other Microsoft cloud services data like Outlook mail, Outlook calendar, OneDrive, tasks, groups, SharePoint, etc. through single endpoint URL (

In my previous article, you have seen the basic introduction and app registration process for accessing the Microsoft Graph data.

The data can be extracted by the following flow.

  • Get access token for authentication.
  • Access the Microsoft services data over graph API.

Acquire Access Token

There are two ways to get an access token. Here, I am only focusing on single approach. In this approach, we will be getting the access token using authentication server URL (<>/oauth2/authorize) with GET method.

The parameters passed for getting the token are,

  • Client ID
  • Response type
  • Resource URL
  • Redirect URL

Fire the constructed URL in the browser using window.location script, and the page will be redirected to the redirect URL specified. In the response URL, the access token will be available. The response URL will be redirect URL specified for redirection.

  1. function requestToken() {   
  2.   var clientId    = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';  
  3.   var replyUrl    = '';   
  4.   var resource = "";  
  5.   var authServer  = '';    
  6.   var responseType = 'token';   
  8.   var url = authServer +   
  9.             "response_type=" + encodeURI(responseType) + "&" +   
  10.             "client_id=" + encodeURI(clientId) + "&" +   
  11.             "resource=" + encodeURI(resource) + "&" +   
  12.             "redirect_uri=" + encodeURI(replyUrl);   
  14.   window.location = url;   
  15. }  
Extract the access token from the response URL. The token will be retrieved once the page is redirected. The following code snippet shows retrieving the token.
  1. var token = getParameterByName['access_token'];   
  2. function getParameterByName(name, url) {  
  3.     if (!url) url = window.location.href;  
  4.     name = name.replace(/[\[\]]/g, "\\$&");  
  5.     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),  
  6.         results = regex.exec(url);  
  7.     if (!results) return null;  
  8.     if (!results[2]) return '';  
  9.     return decodeURIComponent(results[2].replace(/\+/g, " "));  
  10. }   
Get the Cloud Data using Graph API 
In the AJAX call, set the required graph URL. In this case, we are trying to extract the calender events of the logged in user, so the graph URL will be outlook calendar events. Set authorization bearer token (access token) in the header property. Using the get method, access the required events for the logged in user.

The graph URL for accessing logged in user events will be

The following snippet shows the extraction of events using Graph API.
  1. $.ajax({  
  2.     url: "",  
  3.     type: "GET",  
  4.     headers: { "Authorization""Bearer "+token },  
  5.     success: function (data) {  
  6.         for(var i=0;i<data.value.length;i++){  
  7.             // extract the event subject, web link, etc.  
  9.         }         
  10.     },  
  11.     error: function (sender, args) {  
  12.         console.log("error");  
  13.     }  
  14. });  


Thus, you learned accessing the Microsoft cloud data using the Microsoft graph URL on SharePoint. In this sample, Office 365 Outlook calendar events are retrieved. Likewise, other Microsoft cloud data can also be accessed.

In the next article, you will see a full sample code of extracting the Microsoft cloud data.