Extending Electronic Reporting Sources: Integration with OneDrive (I)

In this series, I will explain how to extend ER (Electronic Reporting) sources to import from non-default origins (beyond the default options like manual imports or SharePoint). This will also enable us to program batch jobs for continuous integrations. Specifically, I will guide you step by step on how to integrate with a third-party OneDrive, although most of the steps will be similar if you need to integrate with another source.

The standard pattern for sources involves retrieving files from the origin (e.g., a folder, blob storage, path, etc.) and moving them to another location after import. Depending on whether the import was successful, had warnings, or encountered errors, we will move the files to different destinations.

It may be helpful to first read my series on Extending ER destinations: Integrations with oneDrive as I will assume the reader is already familiar with certain concepts. You can find the official Microsoft documentation here.

One drive API

For authentication, I’m using a refresh token. You can read more about that in my previous post.

When I get the access_token, I can list the files in a folder with a GET request to https://graph.microsoft.com/v1.0/me/drive/items/[folderId]/children, where the authorization is the access token. I know the folder ID from the OneDrive URL, but we just need to replace %21 with ! since the ! is URL-encoded.

Finally, we can read the content of the files with https://graph.microsoft.com/v1.0/me/drive/items/[folderId]:/[filename]:/content, also using the access token.

We can get the ID of a file with a GET request to https://graph.microsoft.com/v1.0/me/drive/items/[folderId]/children?$filter=name eq '[filename]'.

And finally, we can move the file using a PATCH request with a JSON body to https://graph.microsoft.com/v1.0/me/drive/items/[fileId].

{
  "parentReference": {
    "id": "[folderDestination]"
  }
}

Connection table/form

We’ll create (or reuse) a form and table for storing the connection credentials. Just make sure the Secret and Token are encrypted in the database.

Form extension

First, let’s add references to ElectronicReporting and ElectronicReportingCore if the model doesn’t already include them.

Next, we can create an extension of ERImportFormatSourceSettings and add a tab for OneDrive integration. We’ll include a boolean to indicate if the source is active, a string (which will later be a lookup for selecting the connection), and four strings for the different folders: Origin, OK destination, Warning destination, and Error destination. Additionally, make sure all fields have the property AutoDeclaration set to Yes so that they can be referenced in code.

Part 2 ->


by

Comments

Leave a Reply

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