Get members from Office 365 Azure Ad using Graph API

azure-ad

1.Create a WPF application

2. Add the below mentioned code in the App.xaml.cs page.

[code lang=”c”]
private static string ClientId = “0b8b0665-bc13-4fdc-bd72-e0227b9fc011″;
private static PublicClientApplication _clientApp ;
public static PublicClientApplication PublicClientApp { get { return _clientApp; } }

static App()
{
_clientApp = new PublicClientApplication(ClientId);
}[/code]

3.In mainwindow.xaml Add a button for calling the graph and a multiline textbox to show the result. I have attached the code below for mainwindow.xaml.cs

[code lang=”c”]
public partial class MainWindow : Window
{
string graphAPIEndpoint = “https://graph.microsoft.com/v1.0/groups/4fd9e6bb-c353-436c-9157-eae04c7b5182/members”; // the guid used here should be the guid of the azure ad we wanted to get member of.
string[] scopes = new string[] { “Directory.Read.All”, “User.Read.All” };

public MainWindow()
{
InitializeComponent();
}

/// <summary>
/// Call AcquireTokenAsync – to acquire a token requiring user to sign-in
/// </summary>
private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
{
AuthenticationResult authResult = null;
var app = App.PublicClientApp;
ResultText.Text = string.Empty;

try
{
authResult = await app.AcquireTokenSilentAsync(scopes, app.Users.FirstOrDefault());
}
catch (MsalUiRequiredException ex)
{
// This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($”MsalUiRequiredException: {ex.Message}”);

try
{
authResult = await app.AcquireTokenAsync(scopes);
}
catch (MsalException msalex)
{
ResultText.Text = $”Error Acquiring Token:{System.Environment.NewLine}{msalex}”;
}
}
catch (Exception ex)
{
ResultText.Text = $”Error Acquiring Token Silently:{System.Environment.NewLine}{ex}”;
return;
}

if (authResult != null)
{
ResultText.Text = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken);
}
}

/// <summary>
/// Perform an HTTP GET request to a URL using an HTTP Authorization header
/// </summary>
/// <param name=”url”>The URL</param>
/// <param name=”token”>The token</param>
/// <returns>String containing the results of the GET operation</returns>
public async Task<string> GetHttpContentWithToken(string url, string token)
{
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
try
{
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(“Bearer”, token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();

return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
} [/code]

Final Result:-

Office365

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *