DEV Community

Jingyi Z.
Jingyi Z.

Posted on

Access User Data from Microsoft Graph

In the previous blog post, you have learned about the basics of PowerPlatform and Microsoft Graph. In this one, we will elaborate more on Graph specifically and provide examples in the field of education regarding how to access user data. After reading this, you will get an idea about how to use Graph to work with users in MS 365 including the required permissions.

Objectives and Prerequisites

Users are the core of most operations in Microsoft 365. Microsoft Graph enables developers full control over the lifecycle of users in Microsoft 365 including creating, updating, and deleting users and listing users in the organization.

Thus, are you interested in...

  • How to get a list of users?
  • How to get details, including multi-media, of a user?
  • how to manage the lifecycle of a user from creation to deletion?

In order to get those objectives, you should have...

  • Basic knowledge of REST services & APIs
  • Basic knowledge of Microsoft Graph
  • Ability to develop with NET Core at the intermediate level
  • Experience using Visual Studio Code at the beginner level
  • Access to a Microsoft 365 tenant

However, you don't have to check all those boxes! Your interest and willingness to learn are the key. Once we go through the following demonstration process, there are additional resources for you to practice and learn more. So, let's go!

Why integrate with education scenarios?

Imagine a teacher who went to a conference and got excited about certain great applications. After she came back, the first thing she need to do is to load the list of students into the app. It doesn't sound like a fun task to do, right? It can also be kind of a barrier to entry at times when there is a large class or a lot of details needed.

image
(from https://pin.it/28Oyt9U)

This scenario doesn't only happen to teachers, but to students or education software developers who may find key pieces of information are typically locked away inside a school Student Information System (SIS). Any time teachers bring a new application into their classroom, they spend time manually importing roster data into the app. Time-consuming, isn't it? And here is where Graph could come in and help.

What can you do with the Microsoft Graph user resource?

The user resource is the gateway to resources related to a user. The user can be the currently signed-in user, or another user if your identity and application have been granted the necessary permissions.

Following are some examples of things you can do with Graph user resources:

  • Manage your organization by creating new users in your organization or updating the resources and relationships for existing users. E.g. Create or delete users in your Azure AD organization, Upload or retrieve a photo for the user, etc
  • Work with calendars and tasks through viewing, querying, and updating user calendars. E.g. Find free meeting times for a set of users, get a list of reminders set on a user's calendar, etc
  • Administer mail and handle contacts by configuring user mail settings and send mail on a user's behalf. E.g. List mail messages and sending new mail, retrieve and update mailbox folders and settings, etc
  • Gather user insights. E.g. Return documents recently viewed and modified by a user, return documents and sites trending around a user's activity, etc

How to access users through Microsoft Graph?

There are two ways you can do it:

  1. Access the signed-in user through the "me" alias at https://graph.microsoft.com/v1.0/me. This alias maps to the same endpoint as /users/{signed-in user's id}.

    To access a specific user, use either their ID or their userPrincipalName. For example:

  2. Microsoft Graph also enables developers to get a list of users. The /users endpoint will return all users in the organization using the Microsoft Graph API. The following code will do the same thing using the Microsoft Graph .NET SDK:

    GraphServiceClient graphClient = GetAuthenticatedGraphClient(...);
    var users = graphClient.Users.Request().GetAsync().Result;

Back to the case

So now after you have a basic idea about Graph's functionality, let's go back to the scenario and see how to get a class of student info from Graph. There are two ways, build a Graph application or access from the Graph webpage.

To begin with, if you haven't used Graph before, developing Microsoft Graph apps requires a Microsoft 365 tenant. You can follow the instructions on the Microsoft 365 Developer Program site for obtaining a developer tenant if you don't currently have a Microsoft 365 account. Then, you'll create an Azure AD application, a .NET Core console application, then finally display the currently signed-in user's details. For specific demo videos and documentations, please check it out here.

Moreover, if you already have an account set up and have some experience with Graph. You can access it from Graph Explore. For example, if you log in as a teacher account, you can have a look a the list of classes. by using this query:

  GET /education/me/classes
  GET /education/users/{id}/classes
Enter fullscreen mode Exit fullscreen mode

Then, it'll return a list of class like below.
image

From there, you can find descriptions of the classes codes for the class you taught and other details. Then, you can copy the class ID of one of those. In the search query, put

  GET https://graph.microsoft.com/v1.0/education/classes/{class-id}/members
Enter fullscreen mode Exit fullscreen mode

Then, you'll get a list of members of that specific class.
image

As a result, you'll get information about students whose data are always updated since it syncs with school's back-end system. Pretty cool right?

Next Steps

Interested in learning more about Graph? We saw this ahead and prepared the following resources for you to explore:

Summary

Good job on reading so far! In this post, we learned what can you do with the Microsoft Graph user resource, how to access user data through Microsoft Graph in different ways, and how to use Graph to solve problems in the realm of education.
image
(from https://pin.it/4JwkcWS)

Blog 3: Creating Microsoft Graph custom connector in Power Apps

Untitled design
(from https://www.canva.com/)

Top comments (0)