top of page
Writer's pictureNakkeeran Natarajan

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


Introduction

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 (https://graph.microsoft.com).


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 (https://login.microsoftonline.com/<yourdomain.onmicrosoft.com>/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. {Ā Ā Ā 

  3. varĀ clientIdĀ Ā Ā Ā =Ā 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';Ā Ā 

  4. varĀ replyUrlĀ Ā Ā Ā =Ā 'https://yourdomain.sharepoint.com';Ā Ā Ā 

  5. varĀ resourceĀ =Ā "https://graph.microsoft.com";Ā Ā 

  6. varĀ authServerĀ Ā =Ā 'https://login.microsoftonline.com/yourdomain.onmicrosoft.com/oauth2/authorize?';Ā Ā Ā Ā 

  7. 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);Ā Ā Ā  Ā Ā 

  13. window.locationĀ =Ā url;Ā Ā Ā }Ā Ā 

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. {Ā Ā 

  4. ifĀ (!url)Ā urlĀ =Ā window.location.href;Ā Ā Ā Ā Ā Ā 

  5. nameĀ =Ā name.replace(/[\[\]]/g,Ā "\\$&");Ā Ā 

  6. varĀ regexĀ =Ā newĀ RegExp("[?&]"Ā +Ā nameĀ +Ā "(=([^&#]*)|&|#|$)"),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  7. resultsĀ =Ā regex.exec(url);Ā Ā 

  8. ifĀ (!results)Ā return null;Ā Ā 

  9. ifĀ (!results[2])Ā return '';Ā Ā 

  10. returnĀ decodeURIComponent(results[2].replace(/\+/g,Ā "Ā ")

  11. );Ā Ā 

  12. } Ā 


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 calendar 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 https://graph.microsoft.com/v1.0/me/calendar/events.


The following snippet shows the extraction of events using Graph API.

  1. $.ajax({Ā Ā Ā Ā Ā Ā 

  2. url:Ā "https://graph.microsoft.com/v1.0/me/messages",Ā Ā Ā Ā Ā Ā 

  3. type:Ā "GET",Ā Ā Ā Ā Ā Ā 

  4. headers:Ā {Ā "Authorization":Ā "BearerĀ "+tokenĀ },Ā Ā Ā Ā Ā Ā 

  5. success:Ā functionĀ (data)Ā {Ā Ā  for(varĀ i=0;i<data.value.length ;i++)

  6. {Ā Ā 

  7. //Ā extractĀ theĀ eventĀ subject,Ā webĀ link,Ā etc. Ā Ā Ā Ā Ā Ā Ā Ā 

  8. }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  9. },Ā Ā Ā Ā Ā Ā 

  10. error:Ā functionĀ (sender,Ā args)Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 

  11. console.log("error");Ā Ā Ā Ā Ā Ā 

  12. }Ā Ā 

  13. });Ā Ā 


Summary

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.Ā 

0 comments

Comments


bottom of page