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, web URL, displayName, CreateddateTime, mail, etc..

CreateddateTime

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 display name, job title, mobile phone, userPrincipalName, etc.

 DisplayName

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

function requestToken() {  
    $.ajax({  
        "async": true,  
        "crossDomain": true,  
        "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  
        "method": "POST",  
        "headers": {  
            "content-type": "application/x-www-form-urlencoded"  
        },  
        "data": {  
            "grant_type": "client_credentials",  
            "client_id": "8baf0301-27df-44b1-b4fe-7911b9a918de", // Provide your app id  
            "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", // Provide your secret  
            "scope": "https://graph.microsoft.com/.default"  
        },  
        success: function(response) {  
            console.log(response);  
            token = response.access_token; // Store the token into global variable  
        },  
        error: function(error) {  
            console.log(JSON.stringify(error));  
        }  
    })  
}  

The successful response returns like below.

API

Now Create the function fetchOffice365Groups() to fetch all the groups from the Office 365 tenant

Code

function fetchOffice365Groups() {  
    $.ajax({  
        method: 'GET',  
        url: "https://graph.microsoft.com/v1.0/groups/",  
        headers: {  
            'Authorization': 'Bearer ' + token,  
            'Content-Type': 'application/json'  
        },  
    }).success(function(response) {  
        var data = response.value;  
        console.log(data);  
    }).error(function(error) {});  
}

The successful response returns like below.

Code

You can iterate JSON data using a for loop and display it on the web page.

Now Create the function fetchOffice365UserFromGroup() to fetch all the users from the particular group from Office 365 tenant.

Code

function fetchOffice365UserFromGroup() {   
    $.ajax({
        method: 'GET',
        url: "https://graph.microsoft.com/v1.0/groups/02bbaf4e-ce87-47cc-bcd5-98cf251dc8f8/members", // pass Office 365 Group Id   
        headers: {
            'Authorization': 'Bearer ' + token,
            'Content-Type': 'application/json'
        },
    }).success(function(response) {
        console.log(response.value);
        var data = response.value;
    }).error(function(error) {});
}

The successful response returns like below.

JSON data

You can iterate JSON data using a for loop and display it on 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.