Implement your custom “Open in OneDrive” action in Dynamics 365 Business Central

Dynamics 365 Business Central

Hi, Readers.
Last month, we have dicussed Cloud Storage and Open in OneDrive in Dynamics 365 Business Central 2021 Wave2 (BC19).

Today I would like to talk about how to implement your custom “Open in One drive” action. Actually this is an example from Microsoft.

First you need to access to OneDrive from Business Central.
The following is extracted from Cloud Storage and Open in OneDrive.

Open the My setting in BC19, you will see an additional item.

Cloud Storage
Specifies a link to explore your Business Central folder in OneDrive. Select the link to open the folder in a new window.

You can click the value of the Cloud Storage field.

Then a login window will pop up.

1. You need always allow pop-ups and redirects from for your browser.

Otherwise, an error will occur.

Failed to acquire an access token.

2. Need admin approval

Business Central Integration to

Business Central Integration to SharePoint needs permission to access resources in your organization that only an admin can grant. Please ask an admin to grant permission to this app before you can use it.

3. You need a license for Office 365, otherwise, OneDrive cannot be opened.

If there is no problem with the verification, the page will jump to OneDrive.

Test video:

Next, let’s add custom “Open in One drive” action to Posted Sales Invoice page.
The key point for Open in OneDrive feature.

DocumentServiceManagement: Codeunit "Document Service Management";

procedure OpenInOneDrive(FileName: Text, FileExtension: Text, InStream: InStream)

Source Code:

pageextension 50999 PostedSalesInvExt extends "Posted Sales Invoice"
                ApplicationArea = Basic, Suite;
                Caption = 'Open in OneDrive';
                ToolTip = 'Copy the file to your Business Central folder in OneDrive and open it in a new window so you can manage or share the file.', Comment = 'OneDrive should not be translated';
                Image = Cloud;
                Enabled = ShareOptionsEnabled;
                Promoted = true;
                PromotedOnly = true;
                PromotedCategory = Category6;
                PromotedIsBig = true;

                trigger OnAction()
                    TempBlob: Codeunit "Temp Blob";
                    DocumentServiceManagement: Codeunit "Document Service Management";
                    InStr: InStream;

                    DocumentServiceManagement.OpenInOneDrive(StrSubstNo(SalesInvoiceName, Rec."No."), '.pdf', InStr);

        ShareOptionsEnabled: Boolean;
        SalesInvoiceName: Label 'Sales Invoice %1';

    trigger OnOpenPage()
        DocumentSharing: Codeunit "Document Sharing";
        ShareOptionsEnabled := DocumentSharing.ShareEnabled();

    local procedure GetInvoice(var TempBlob: Codeunit "Temp Blob")
        ReportSelections: Record "Report Selections";
        RecRef: RecordRef;

        ReportSelections.GetPdfReportForCust(TempBlob, ReportSelections.Usage::"S.Invoice", RecRef, Rec."Sell-to Customer No.");

Test Video:

PS: You can choose Keep both or Overwrite when the file exists.

If you choose Keep both, a new file will be copied.

If you choose Overwrite, the old file will be updated and you can see version history in OneDrive.

Other information from Microsoft:

Update 2022/05/18: Implement your custom “Share With OneDrive” action in Dynamics 365 Business Central


Hope this will help.

Thanks for reading.



Copied title and URL