By default, you can export ER generated files to your computer, to a tab in your browser, to an email, to an archive (either the default SharePoint or the default Azure Storage), to a printer, and even to Power BI (though I’m not sure how this works, it seems to pass through SharePoint). Most of the time, these destinations are sufficient, but you may need to extend them. For example, you might need to send files to a specific third-party Azure Blob Storage or FileShare, to an SFTP, or, as we will see in this series, to a third-party OneDrive.
I’m going to use the Microsoft documentation as a base, but instead of exporting to a local folder, we are going to export the documents to OneDrive.
OneDrive API
You can skip this part if you need to integrate with another destination.
Learning how to use the OneDrive API wasn’t easy for me, mainly because I’ve never worked with a redirect URI before and I tried to jump directly to Postman without fully understanding the steps. Since OneDrive is just an example, I’ll explain it briefly.
First, I created an app in Azure with a redirect URI set to https://oauth.pstmn.io/v1/browser-callback. Then, I added Files.ReadWrite.All permissions and created a secret. This app will have permissions to all my drive, so this could be a security concern (use your best judgment).
Then with my browser I went to https://login.microsoftonline.com/common/oauth2/v2.0/authorize
?client_id=[AppID]
&scope=Files.ReadWrite.All%20offline_access
&response_type=code
&redirect_uri=https://oauth.pstmn.io/v1/browser-callback
I was then redirected to: https://oauth.pstmn.io/v1/browser-callback
?code=[Code]
I copied the code and made a GET request to https://login.microsoftonline.com/consumers/oauth2/v2.0/token with Postman, where I got the refresh code:
Finally, I used this refresh token for the token request:
With this Bearer token (which lasts 1 hour), I can create files on my OneDrive by pointing to: https://graph.microsoft.com/v1.0/me/drive/items/[IDOfMyFolder]:/[FileName.extension]:/content:
So, what’s important here is that to connect to OneDrive from D365FO (to get the token), I’ll need:
- The Client ID of the app
- The secret of the app
- The refresh token (The code I got from the first API call)
And once I have the token, I’ll just need the ID of the folder where I want to export the files. Although there are ways to work directly with the file route, I’m not going to implement it this way.
Connection table/form
I’ve created the following table and its form in D365FO. It has the three fields we need to generate a connection with OneDrive. Two of these fields are encrypted; I have explained in another post how to create encrypted fields.
The only difference is that because the token is too long, the second parameter of the method for the refresh token should be str
instead of Name
(max. 60).
public edit TST2_RefreshToken TST2_RefreshTokenEdit(boolean _set, str _value)
{
return Global::editEncryptedField(this, _value, fieldNum(TST2_OneDriveCon, RefreshToken), _set);
}
Form extension
Let’s make sure our model has references to ElectronicReporting
and ElectronicReportingCore
.
Then we can start by extending the ER destinations form ERFormatDestinationSettings
and add three fields:
- A checkbox to indicate if this destination is enabled.
- A string for the OneDrive folder ID.
- Another string that will be used as a lookup to the table we’ve created with connection credentials.
Set the property Auto Declaration
to Yes
for all of them so we can reference them in the code.
After compiling, if we go to the ER destinations, we should already see this new tab. Of course, it currently has no functionality yet.
Leave a Reply