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


Introduction

In this article series, we are focusing on accessing the Office 365 and Microsoft Cloud Services data, using graph REST API calls.


In this article, let us look at the samples for accessing the office 365 outlook events and outlook messages with the help of Microsoft graph API.


Microsoft Graph exposes Office 365 and other Microsoft cloud services data like outlook mail, outlook calendar, one drive, tasks, groups, SharePoint, etc. through single endpoint URL (https://graph.microsoft.com).


In my previous articles, you will have seen the basic introduction, app registration process, getting access token for the authentication and accessing Microsoft Services data over graph API, using AJAX call.


Get data

There will be two calls to access Microsoft Cloud data. First call is to acquire the access token for any further authorisations and the second call is to access the data with the access token.

  1. On page load, using the condition; check if the URL consists of an access token parameter. Normally on the first page load, there will not be any parameters present on the URL).

  2. If an access token is not available, acquire the access token with the help of an authentication Server URL with the necessary parameters. This step was explained in my previous article with detailed steps. On success, the page will be redirected to the redirected URL specified with an access token.

  3. If the token is available on the URL, decode the response URL and extract the token.

  4. Once the token is extracted and available, access Microsoft Cloud data with an appropriate graph URL. The access token, which has been acquired, should be passed in the header for the authorisations while accessing the data with graph URL.


Note

The acquired token will be valid for one hour.


JavaScript code snippet given below shows the entire functionality to retrieve the calendar events with the explained authentication model given above.

  1. $(document).ready(function()

  2. {  

  3. var token = getParameterByName['access_token'];   

  4. if(token == null || token == undefined)

  5. {  

  6. // On Page load or if token not available on the URL.         

  7. requestToken();      }  

  8. else{  

  9. // Token available, extract the data        

  10.  $.ajax({              

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

  12. type: "GET",              

  13. headers: { "Authorization": "Bearer "+token },              

  14. success: function (data) {  

  15. var messages = reactHandler.state.messages;  

  16. // Extract the data

  17. for(var i=0;i<data.value.length ;i++){  

  18. if(data.value[i].subject != null)

  19. {                          

  20. messages.push({                              

  21. subject:data.value[i].subject,                              

  22. weblink:data.value[i].webLink                           

  23. })                      

  24. }                   

  25. }                  

  26. reactHandler.setState({                      

  27. messages: messages                  

  28. });              

  29. },              

  30. error: function (sender, args) {                  

  31. console.log("error");              

  32. }          

  33. });       

  34. }  

  35. });  

  36. // Extract the access token from the URL

  37. function getParameterByName(name, url) {  

  38. if (!url) url = window.location.href;      

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

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

  41. results = regex.exec(url);  

  42. if (!results) return null;  

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

  44. return decodeURIComponent(results[2].replace(/\+/g, " "));  }  

  45. // Get the token using the redirect URL

  46. function requestToken() {   

  47. var clientId    = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';  

  48. var replyUrl    = 'https://nakkeerann.sharepoint.com';   

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

  50. var authServer  = 'https://login.microsoftonline.com/nakkeerann.onmicrosoft.com/oauth2/authorize?';    

  51. var responseType = 'token';   

  52. var url = authServer +    "response_type=" + encodeURI(responseType) + "&" +   

  53. "client_id=" + encodeURI(clientId) + "&" +   

  54. "resource=" + encodeURI(resource) + "&" +   

  55. "redirect_uri=" + encodeURI(replyUrl);    /

  56. / Redirect to the URL, which will have access token on successful authentication   window.location = url;   

  57. }  


Summary

Thus, you have seen accessing Microsoft Cloud data, using Microsoft graph URL on SharePoint. In this sample, Office 365 Outlook calendar events are retrieved.

Recent Posts

See All