Microsoft 365: Get user information from Azure

Automatically translated from English

Many companies are using Microsoft 365 (former Office 365). Besides providing access to Office applications, Microsoft provides an Azure-based Active Directory, allowing users to log in to access services such as Microsoft Sharepoint or OneDrive using a Web Browser.

All Microsoft services use the same Single Sign-On, based on the email address. From 4D v19 R6, 4D NetKit allows you to join this concept, allowing users to log in to your application using their usual company credentials.

Besides allowing Single Sign-On, it will enable to retrieve information for the authenticated user such as email, department, and phone number. If permitted by the administrator, you can even retrieve such details on colleagues from the same company.

Let’s see how it works.

First, to access a user’s information, you’ll need to get access on their behalf and use the New OAuth2 provider command to create your OAuth2 provider:

$param:=New object()
$param.name:="Microsoft"
$param.permission:="signedIn"
$param.clientId:="XXX"
$param.redirectURI:="http://127.0.0.1:50993/authorize/"
$param.scope:="https://graph.microsoft.com/.default"
// Create new OAuth2 object
$tokenProvider:=New OAuth2 provider($param)

When your OAuth2 provider is ready, use the New Office365 provider command to instantiate an object that will manage the access to your Microsoft 365 account through the Microsoft Graph API:

var $Office365 : cs.NetKit.Office365Provider
$Office365:=New Office365 provider($tokenProvider)

user information

You can get the user information like displayName, givenName, and more.

If you are connected in Sign In mode, you can get the current user information:

$currentUser:=$Office365.user.getCurrent()
//$currentUser=
//{displayName:My Name,givenName:Name,mail:myname@mycompany.com,...}

If you belong to Azure AD “work or school” account, you can access the information of a specific user of your organization:

$user:=$Office365.user.get("myname@mycompany.com")

or access the list of all the users of your organization:

$users:=$Office365.user.list()

Check out the documentation for more details, and don’t hesitate to share with us your experience on the 4D forum.

Fabrice Mainguené

• Product Owner •

Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.

After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.