A simple way to connect Business Central with One Drive

Hello, today I want to share with you a simple way to connect Business Central with One Drive and Business Central.

As you may have already noticed, I’m a bit of a fan of Azure Functions. We use the Azure project to connect to the Graph API and with it, in turn, to our One Drive.

We could update the Azure project to connect to SharePoint or maybe Dropbox or some SFTP.

In the following links are the repositories used:

Business Center.
AzureFunctions.

Azure Functions

For the development of the Azure Functions Project, we have used Microsoft.Graph Nuget package

FileModel:

This is the model that we have created with the information that we will store from the OneDrive items.

As you can see I created 2 similar fields, ExtensionType1 and ExtensionType2.

This was out of curiosity to see how he behaved. I recently installed Github Copilot, and it automatically created this code for me “file.File.MimeType.Split(‘/’)[1]” to get the extension when I was creating the Azure Functions, seeing that it worked I left it in the model.

ConfigurationsValues:

We use this table to get all the necessary Oauth2 and Graph API settings like UserID and FolderID.

FileDownloader:

This class will help us download the file and additionally convert it to a Byte[]

GetItemsByFolder:

Next, our Azure Functions is in charge of making the connection with the Graph API and taking the elements of a specific folder.

I would say that it has 4 parts:

  • 1) Connect with Oauth2 to the Graph API.
  • 2) List the items given a UserID and a FolderId.
  • 3) Convert the content to a Byte[]
  • 4) Store the information in our FileModel class that will be the one we will send as JSON when executing the function.

Specials Notes:

  1. Runtime Error:

In the tests I did, I realized that I was getting the following error:

Code: BadRequest Message: /me request is only valid with delegated authentication flow.

To fix it, I had to replace: _graphServiceClient.Me.Drive with _graphServiceClient.Users[UserID].Drive

To get my UserID, use the following Web Site: https://developer.microsoft.com/en-us/graph/graph-explorer

2. Business Central Folder:

For this post and demonstration purposes, I only worked with a single folder called BusinessCentral inside my OneDrive, which I obtained its Id in the following way.

3. Oauth2:

We use Outh2 as an authentication mechanism, because my account has double-pass verification and additionally because it is an API-type project.

With Outh2, we need 3 values to configure authentication:
1) Tenant.
2) ClientSecret.
3) Clientid.

To obtain them, we need to create an App Registration.

This previous post explains how to configure it, but we will need to change 2 things: The Redirect URL in the application registration and the Permissions

  • Redirect Url:

We select Mobile and Desktop Applications

We select the first check, and in Customs Redirects URIs we put “http://localhost”

  • Permissions:

In the Microsoft Learning Path you can find more information about how to connect to the Graph API and other configurations.

4. Use of Variables:
All the variables that were used in the project were stored in the Azure configurations.

Business Central

Tables

OneDrive

In Business Central I call it OneDrive, but it’s exactly the same model we use in the Azure project FileModel

Codeunits:

GetFilesFromOneDrive:

This is the core of the project in AL, here we read our Azure Functions API, undo the result and store it in Business Central.

I have to confess, it took me a while to convert the data to Blob but I finally managed it with this wonderful line of code:

Base64Convert.FromBase64(FileArrayBase64, OutStream);

DownloadFromCloud:

With this process, we download the content to our local computer by clicking on the file name.

Pages:

This page will show us all the elements that we have in our folder configured in One Drive.

Testing:

As we have activated the OnDrillDown trigger in the File Name field, when we click on it, we will be asked if we want to download the said file to our pc.

Video:

Conclusion

This was a small project to demonstrate the power of Azure to integrate with cloud file management services like One Drive.

The project undoubtedly has many limitations and is not intended to be used as a final project.

Among the improvements that could be integrated:
1) Create a module to Upload/Update files.
2) Remove the “OneDrive.DeleteAll();” which is used in the Reload and improve the synchronization logic. (A bad practice to recreate everything again).

If you want, feel free to create PR and make improvements.

Finally, before finishing, last night I saw a Tweet that I liked, of a project that is being created with native APIs in Business Central to connect to Sharepoint.

I hope this helps you.

Leave a Reply

Your email address will not be published. Required fields are marked *