Fetch Office 365 Groups Programmatically Using Microsoft Graph API

In this article, I have explained how to retrieve Office 365 user groups using Microsoft Graph API. In my previous articles, I have already written about how to fetch access tokens to authorize your web application with Microsoft Graph. Use the below link to learn about Microsoft Graph,
Let's get started to retrieve Office 365 Groups and Users from Office 365 using Microsoft Graph API.
 
Retrieve All The Office 365 Groups
 
EndPoint - https://graph.microsoft.com/V1.0/groups
 
This endpoint helps you to retrieve all the office 365 groups from your tenant. 
 
It returns the ID, webUrl, displayName, CreateddateTime, mail, etc..
 
Fetch Office 365 Groups Programmatically Using Microsoft Graph API
 
If you want to retrieve the particular office 365 group members use the following endpoint. 
 
EndPoint - https://graph.microsoft.com/V1.0/groups/{Groupid}/members
 
It retrieves all the users inside the office 365 group with some properties like displayName, job title, mobile phone, userPrincipalName, etc.
 
Fetch Office 365 Groups Programmatically Using Microsoft Graph API
 
Let's programmatically fetch office 365 Groups and Extract users from office 365 groups using Microsoft Graph API.
 
Step 1
 
Fetch access token to authenticate your web application with Microsoft Graph API.
 
Code
  1. function requestToken() {  
  2.     $.ajax({  
  3.         "async"true,  
  4.         "crossDomain"true,  
  5.         "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  
  6.         "method""POST",  
  7.         "headers": {  
  8.             "content-type""application/x-www-form-urlencoded"  
  9.         },  
  10.         "data": {  
  11.             "grant_type""client_credentials",  
  12.             "client_id ""8baf0301-27df-44b1-b4fe-7911b9a918de"//Provide your app id  
  13.             "client_secret""tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY="//Provide your secret  
  14.             "scope ""https://graph.microsoft.com/.default"  
  15.         },  
  16.         success: function(response) {  
  17.             console.log(response);  
  18.             token = response.access_token; //Store the token into global variable  
  19.         },  
  20.         error: function(error) {  
  21.             console.log(JSON.stringify(error));  
  22.         }  
  23.     })  
  24. }  
The successfull response returns like below,
 
Fetch Office 365 Groups Programmatically Using Microsoft Graph API 
 
Now Create the function fetchOffice365Groups() to fetch all the groups from the Office 365 tenant
 
Code
  1. function fetchOffice365Groups() {  
  2.     $.ajax({  
  3.         method: 'GET',  
  4.         url: "https://graph.microsoft.com/v1.0/groups/",  
  5.         headers: {  
  6.             'Authorization''Bearer ' + token,  
  7.             'Content-Type''application/json'  
  8.         },  
  9.     }).success(function(response) {  
  10.         var data = response.value;  
  11.         console.log(data);  
  12.     }).error(function(error) {});  
  13. }  
The successfull response returns like below,
 
Fetch Office 365 Groups Programmatically Using Microsoft Graph API 
 
You can iterate JSON data using for loop and display it in the web page.
 
Now Create the function fetchOffice365UserFromGroup() to fetch all the users from the particular group from office 365 tenant.
 
Code
  1. function fetchOffice365UserFromGroup() {  
  2.     $.ajax({  
  3.         method: 'GET',  
  4.         url: "https://graph.microsoft.com/v1.0/groups/02bbaf4e-ce87-47cc-bcd5-98cf251dc8f8/members", //pass Office 365 Group Id  
  5.         headers: {  
  6.             'Authorization''Bearer ' + token,  
  7.             'Content-Type''application/json'  
  8.         },  
  9.     }).success(function(response) {  
  10.         console.log(response.value);  
  11.         var data = response.value;  
  12.     }).error(function(error) {});  
  13. }  
The successful response returns like below,
 
Fetch Office 365 Groups Programmatically Using Microsoft Graph API 
 
You can iterate JSON data using for loop and display it into your web page.
 
Conclusion
 
So folks, now you learned how to retrieve office 365 groups and members programmatically using Microsoft Graph API with a simple jQuery.