top of page

How to use Microsoft Graph API in Business Central

Updated: Aug 17, 2020

Microsoft Graph API is very powerful Restful Web API, and a gateway to data in Microsoft 365. Data from diffrent applications in Microsoft 365 like SharePoint, Teams, Calenders, OneDrive etc. can be accessed using very simple web queries.

Now Business Central data can also be accessed by using Microsoft Graph API from other applications using the same API. More information can be found at Working with the Dynamics 365 Business Central API in Microsoft Graph

How ?

We can use Microsoft Graph API by following 5 simple steps.

  1. Register your Application in Azure Portal.

  2. Add Redirect URI(s)

  3. Add Permissions to the Application

  4. Add Client Secret to the Application

  5. Get Authetication Token

  6. Quering Data

Register your application

1) Open and sign-in using your organization credientials.

2) Search for App Registrations in the search bar and select App registrations.

3) Click on New registration button.

4) Enter your Application Name, and click on Register button.

5) You should be in the following screen after clicking the Register button.

Add Redirect URI(s)

1) Click on Add a Redirect URI link

2) Click on Add a platform and Select the Web option in the Configure platforms panel

3) Update the Redirect URI. You should replace localhost:8080 with your web servers host name. Then click the Configure button.

Add Client Secret to the Application

1) Select Certificates & secrets button in the left navigation menu and click the New client secret button in right panel.

2) You will get the following dialouge. Enter Description, and select Expires option. Click the Add button.

3) The above step creates a client secret. You need to copy this value and save it somewhere. Once this page is closed, you cannot copy this value again.

Add Permissions

1) Click on API permissions button from the left navigation menu

2)You will see the following screen on the right side of the navigation menu. Click on Add a Permission button under Configure permissions.

3) Select the Microsoft Graph option in Request API Permissions.

4) Select the Delegated permissions option.

5) Search for "Files" in the search box, and select Files.ReadWrite.All option in the list and click on the Add Permissions button.

6) After completing the above steps you should be able to see the following permissions.

API End Point

API Endpoints can be found after selecting Overview, by clicking on Endpoints button. we are going use OAuth 2.0 authorization endpoint (v2) for get AuthToken.

Get Authetication Token

We are using OAuth2 codeunit to get Autherization Token, this codeunit also has some useful functions related to OAuth2 authentication.

In the below code, the GetAccessToken function gets the AuthToken using OAuth2 codeunit.

codeunit 50115 "Graph API Helper"
        OAuth2: Codeunit OAuth2;
        ClientIdTxt: Label '96e2efa1-a6fb-4f04-97d9-1f9ac9c15917', Locked = true;
        ClientSecret: Label '44Qi99bD4EC4S27~_.5htAp1o_lLd7tfBg', Locked = true;
        ResourceUrlTxt: Label '', Locked = true;
        OAuthAuthorityUrlTxt: Label '', Locked = true;
        RedirectURLTxt: Label 'http://localhost:8080/BC160/OAuthLanding.htm', Locked = true;
        OneDriveRootQueryUri: Label '', Locked = true;

    procedure GetAccessToken(): Text
        PromptInteraction: Enum "Prompt Interaction";
        AccessToken: Text;
        AuthCodeError: Text;

        if (AccessToken = '') or (AuthCodeError <> '') then


Quering Data

In the below code, the GetOneDriveFiles function is quering data from OneDrive's root folders and files using Microsoft Graph API.

    procedure GetOneDriveFiles(): JsonObject
        Client: HttpClient;
        RequestMessage: HttpRequestMessage;
        ResponseMessage: HttpResponseMessage;
        JsonResponse: JsonObject;
        AccessToken: Text;
        JsonContent: Text;
        AccessToken := GetAccessToken();

        Client.DefaultRequestHeaders().Add('Authorization', StrSubstNo('Bearer %1', AccessToken));
        Client.DefaultRequestHeaders().Add('Accept', 'application/json');

        if Client.Send(RequestMessage, ResponseMessage) then
            if ResponseMessage.HttpStatusCode() = 200 then begin


This is a very simple example to retrieve data from OneDrive. Using the Microsft Graph API you can access almost any data in Microsoft 365. For example, using this Microsoft Graph API we can integrate SharePoint documents with Sales Order, Purchase Order pages, show e-mail communications in the Customer page etc.

Happy Coding!!!

You can download complete sample code from GitHub.

#MsDyn365 #MsDyn365 #BusinessCentral #DynamicsNAV #ALLanguage

6,725 views7 comments
bottom of page