How to get Office 365 Groups using Microsoft Graph API

In this tutorial, we will discuss how to get Office 365 groups using Graph API.

Also, we will see how to create the security token in Azure which we can use inside the console application.

I will also show, how to get user profile details using Graph API.

As we know Microsoft has provided an easy way to get and test the API in Microsoft graph explorer. You can find the URL here.

Here I just tested the graph api to get the all groups.

Create the security token in Azure cloud

Before going to start, verify you have an account to access Microsoft Azure. You can log in the azure account through given URL https://portal.azure.com/#home

Create the security token in Azure cloud

Step 1: Login to the Azure account

how to Create the security token in Azure cloud

Step 2: Next go to the App registrations and click on New registration.

Azure App registrations

Step 3: Next give your application name and choose the account type as below screenshot. Next click on Register.

New Azure App registrations

Step 4: Once you register your account, you will get three keys which will help you to use in your application for security purpose.

Azure app registration using graph api

Step 5: Next go to the API Permission and Add permission.

request api permission azure

Step 6: Next go to Application permission to provide the permission.

request api permission microsoft azure

Step 7: Here you can check the permission and click on Add Permission.

How to get Office 365 Groups using Microsoft Graph API

Step 8: Next click on grant permission to approve the permission.

How to get Office 365 Groups using Graph API

Step 9: After you allow the permission. it will come like below screenshot.

Get Office 365 Groups using Microsoft Graph API

Step 10 : Next go to the Certificate and Secrets to generate a secret key.

Get User Profile Details using Graph API

Step 11: While creating the secret key, you have to provide the Expires of your key.

Get User Profile Details using Microsoft Graph API

Step 12: After i add the key, below is my Secret key which is created. So now I am ready to use this API and keys in my application.

microsoft graph get group by name

Get Office 365 Groups using Microsoft Graph API

Now, we will create a console application and then we can see how to get office 365 groups using Graph API.

Step 13: Go to the Visual Studio -> Create a console application and use the below code to get the groups.

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConnectWithSharePointOnline
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                GetUsersAsync().GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        public async static Task GetUsersAsync()
        {
            var clientId = "c42f28e2-6d35-4abf-abd5-fe7b2874ff47";
            var tenantId = "a22d2591-6b37-4be3-b226-22d374c8818b";
            var SecretKey = "_718_H0sf8l4P6U~1c0EGSawbIyH.Ev-H~";
            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithTenantId(tenantId)
                .WithClientSecret(SecretKey)
                .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var groups = await graphClient.Groups.Request().Select(x => new { x.Id, x.DisplayName,x.Members }).GetAsync();
            foreach (var group in groups)
            {
                Console.WriteLine($"{group.DisplayName}, {group.Id},{ group.Members}");
            }
        }
    }
}

Step 14: You will get a reference error, so please install the below NuGet package in your application to avoid reference issue.

Install-Package Microsoft.Graph  
Install-Package Microsoft.Graph.Auth -IncludePrerelease  

Step 15: Below is the output of the code .

microsoft graph api get group by name

Get User Profile Details using Graph API

We can also easily get the user profile details using Graph API.

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConnectWithSharePointOnline
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                GetUsersAsync().GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        public async static Task GetUsersAsync()
        {
            var clientId = "c42f28e2-6d35-4abf-abd5-fe7b2874ff47";
            var tenantId = "a22d2591-6b37-4be3-b226-22d374c8818b";
            var SecretKey = "_718_H0sf8l4P6U~1c0EGSawbIyH.Ev-H~";
            var clientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantId)
    .WithClientSecret(SecretKey)
    .Build();

            // Create ClientCredentialProvider that will manage auth token for you
            var authenticationProvider = new ClientCredentialProvider(clientApplication);
            var graphClient = new GraphServiceClient(authenticationProvider);

            // Call Graph API
            var user = await graphClient.Users["rswain44@pikasha12.onmicrosoft.com"].Request().GetAsync();
Console.WriteLine(user.DisplayName + "\n" + user.JobTitle + "\n" + user.MobilePhone + "\n" + user.Mail + "\n" + user.PreferredLanguage);
        }
    }
}
Get User Profile Details using Graph API

Please try and let me know if you are facing any issue.

You may like following tutorials:

In this tutorial, we learned how to get Office 365 Groups using Microsoft Graph API and also we checked how to get the security token in Azure and how to get user details using Graph API.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 7+ years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

>