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.
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
Step 2: Next go to the App registrations and click on New registration.
Step 3: Next give your application name and choose the account type as below screenshot. Next click on Register.
Step 4: Once you register your account, you will get three keys which will help you to use in your application for security purpose.
Step 5: Next go to the API Permission and Add permission.
Step 6: Next go to Application permission to provide the permission.
Step 7: Here you can check the permission and click on Add Permission.
Step 8: Next click on grant permission to approve the permission.
Step 9: After you allow the permission. it will come like below screenshot.
Step 10 : Next go to the Certificate and Secrets to generate a secret key.
Step 11: While creating the secret key, you have to provide the Expires of your key.
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.
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 .
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["[email protected]"].Request().GetAsync();
Console.WriteLine(user.DisplayName + "\n" + user.JobTitle + "\n" + user.MobilePhone + "\n" + user.Mail + "\n" + user.PreferredLanguage);
}
}
}
Please try and let me know if you are facing any issue.
You may like following tutorials:
- How to get logged in user count in SharePoint
- Add Modern SharePoint Site Page in Microsoft Teams
- How to change Microsoft Teams Background
- The file is not digitally signed PowerShell script
- Share blog posts on Twitter and LinkedIn using Power Automate or MS Flow
- SharePoint online spfx script editor web part
- How to get the total number of users from SharePoint group using JSOM
- How to Create a Tiles View using SharePoint Online modern list view customization
- Create user profile card using SharePoint Online modern list view customization using JSON
- SharePoint modern list view customization example
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.
Rajkiran is currently working as a SharePoint Consultant in India . 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).