Dynamics 365 Business Central: retrieving Azure AD User informations via AL

I saw many partners in the past days asking how to retrieve informations about the current Dynamics 365 Business Central user via AL code.

In Dynamics 365 Business Central, users are retrieved from Azure Active Directory and if you check the User Card page you can see only few details on it (full name and contact email):

But in your organization’s Azure Active Directory, you can specify other details connected to a user acccount, like for example job title, address, phone number (business and mobile), office location and more.

How can you retrieve these informations?

Dynamics 365 Business Central has not a native method to retrieve user’s informations from Azure Active Directory, but you can use Microsoft Graph APIs for that. Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources.

To read from or write to a resource such as a user or an email message, you construct a REST request that looks like the following:

{HTTP method} https://graph.microsoft.com/{version}/{resource}?{query-parameters}

In order to use Microsoft Graph API, you need to register an application into Azure Active. Registration integrates your app with the Microsoft identity platform and establishes the information that it uses to get tokens. Instructions on how to register the application can be found here.

How can you call Graph APIs from AL

To retrieve the informations of the current user (its profile in Azure AD) you can call the following Graph API endpoint with a simple GET method:

https://graph.microsoft.com/v1.0/me

To do that from AL code, you need to perform the following steps:

  1. Acquire a token
  2. Call the Graph API

To acquire a token from AL code, you can use the Oauth2 codeunit from Dynamics 365 Business Central by using the following procedure:

where

When you have the token, you can then call the Graph API endpoint by performing a GET operation by using the HttpClient object:

CreateRequest('https://graph.microsoft.com/v1.0/me', AccessToken);

A generic procedure to do that is the following:

Result of this?

The Graph API will give you a JSON with all the user’s details:

Microsoft Graph is powerful and you can do a lot with the exposed APIs. I suggest to remember that it exists 🙂

Leave a comment

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