Office 365 - Microsoft Graph - CodeBase to fetch all Office 365 groups using CSOM

Office 365 – Microsoft Graph – Part 4 – Fetching all Office 365 groups using CSOM- Codebase

Hi All,

Let’s continue our Microsoft Graph show. In last article we discussed about Azure Access Token, we require for calling Graph APIs.

If you missed the previous articles, here are those, please have a look once.

Office 365 – Microsoft Graph and Graph Explorer

Office 365 – Azure Active Directory – Registering/Creating new Azure App – detailed steps

Office 365 – Microsoft Graph beginning – Part 1

Office 365 – Microsoft Graph – Part 2 – Granting permission to Azure Apps to use Microsoft Graph APIs using CSOM

Office 365 – Microsoft Graph – Part 3 – Azure Access Token: to call Graph APIs from CSOM

We are discussing one use case – listing all O365 groups. We have prerequisites are in place:

  1. We choose development environment – Visual Studio 2017 + CSOM SDK
  2. We have registered App in Azure AD
  3. We have Azure App Id and generated client secret key
  4. We have configured permissions for above created app to use Graph APIs for fetching Office 365 groups – we have granted “Read All” permissions for groups
  5. In last article we discussed how to get “Azure Access Token”

Now time to call actual GRAPH API and see the result.

Following are the detailed steps:

Start the Visual Studio 2017, create console application, let’s say “knowledge-junction” as

Figure 1: Office 365 – Microsoft Graph – Console Application – to fetch all Office 365 groups

Install require packages using NuGet manager

Open the NuGet manager as

Figure 2: Office 365 – Microsoft Graph – Installing required packages for calling Graph APIs

We will require following packages:

  • Microsoft Graph – Microsoft.Graph  – client library allows to call us Office 365, Azure AD, and other Microsoft Services through single unified developer experience as

Figure 3: Office 365 – Microsoft Graph – Installing “Microsoft.Graph” client library

Click on “Install” button and please make sure the package installed successfully. We can make sure from OutPut window. And also verify in references section in Solution Explorer as

Figure 4: Office 365 – Microsoft Graph  – references are added for ‘Microsoft Graph” package

  • Microsoft Identity Client Active Directory – Contains the binaries of the Active Directory Authentication library (ADAL). ADAL provides easy to use authentication functionality for .NET based client by taking advantage of Windows Server Active Directory and Azure Active Directory.

Figure 5: Office 365 – Microsoft Graph – Installing “Microsoft Identity Model Client Active Directory” package

Click on “Install” button and please make sure the package installed successfully. We can make sure from OutPut window. And verify in references section in Solution Explorer as

Figure 6: Office 365 – Microsoft Graph – references are added for “Microsoft.IndentityModel.Clients.ActiveDirectory” assembly

Once we have required packages are in place, we can call “Microsoft Graph” APIs.

We need to override the method – AuthenticateRequestAsync(HttpRequestMessage request) from IAuthenticationProvider interface – which adds the “Azure Access Token” to header of HttpRequestMessage request object. We discussed this in our last article.

To achieve this we will add new class in our solution which will be derived from IAuthenticationProvider as

Figure 7: Office 365 – Microsoft Graph – Overriding “AuthenticateRequestAsync” method from IAuthenticationProvider

Next step is to create instance of GraphServiceClient, this class is part of “Microsoft Graph” library as

Once we have instance of GraphServiceClient instance ready we can fetch groups as

Complete code:

Figure 8: Office 365 – Microsoft Graph – CodeBase to fetch all Office 365 groups using CSOM

References:

Intro to the Microsoft Graph .NET Client Library

Microsoft Graph documentation

IAuthenticationProvider

HttpRequestMessage

AuthenticationContext

AuthenticationResult

Thanks for reading 😊

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more.

If you have any suggestion / feedback / doubt, you are most welcome. Stay tuned on Knowledge-Junction, will come up with more such articles

Advertisements

Comments

2 comments on “Office 365 – Microsoft Graph – Part 4 – Fetching all Office 365 groups using CSOM- Codebase”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.