Dynamics 365 Business Central: How to save reports directly to SharePoint via AL (Graph API) – Manual and Automatic

Dynamics 365 Business Central

Hi, Readers.
We discussed Dynamics 365 Business Central: How to import/read files from SharePoint (document library) to Business Central via AL (Graph API) and Dynamics 365 Business Central: How to upload files from Business Central to SharePoint (document library) via AL (Graph API) last month. Today I would like to continue to discuss in depth the questions that were asked later, how to save reports directly to SharePoint via AL. (The first half is the same as before, you can skip it if you have already configured it)

We also use Microsoft Graph API this time.
Microsoft Graph API: Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.

Registering your application establishes a trust relationship between your app and the Microsoft identity platform. This part of the setup is the same as in Using OAuth to connect Business Central APIs and Web Service in Postman. Just the API permissions are different.

To configure application permissions for the app in the app registrations experience on the Microsoft Entra admin center, follow these steps:

  • On the application’s API permissions page, choose Add a permission.
  • Select Microsoft Graph > select Application permissions.
  • In the Select Permissions dialog, choose the permissions to configure to the app.

This time I only added Microsoft Graph -> Application -> Files.ReadWrite.All.

PS: Upload or replace the contents of a driveItem permission

Permission typeLeast privileged permissionsHigher privileged permissions
Delegated (work or school account)Files.ReadWriteFiles.ReadWrite.All, Sites.ReadWrite.All
Delegated (personal Microsoft account)Files.ReadWriteFiles.ReadWrite.All
ApplicationFiles.ReadWrite.AllSites.ReadWrite.All

Then we can test in Postman to see if it is accessible.

Enter the following info in Postman. (You can use variables instead to keep sensitive data secure)

Access Token URLhttps://login.microsoftonline.com/d8f36038-1f93-4543-affc-5dc92b6ee871/oauth2/v2.0/token (Change it to your tenant ID)

Client ID: a80c03cf-6ffa-4b6e-b2c8-6005310d3d87

Client Secret: fME7Q~cAaSBhXMGZoHY3ei64nn1fxGpqF42mh

Scope: https://graph.microsoft.com/.default (Note that Scope is different from BC)

Cilent Authentication: Send client credentials in body

And my test SharePoint site:

https://2qcj3x.sharepoint.com/sites/BCShareFiles

Documents:

Business Central Folder:

Then we can get the GUID of the SharePoint Online site via adding the following _api/site/id at the end of the Site URL:

https://2qcj3x.sharepoint.com/sites/BCShareFiles/_api/site/id

SharePoint (site) id: 5b3b7cec-cbfe-4893-a638-c18a34c6a394

At this time we can get document libraries information.
Test Endpoint:

https://graph.microsoft.com/v1.0/sites/{site-id}/drives

For example,

https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drives

Documents (drive) id:

 b!7Hw7W_7Lk0imOMGKNMajlK0n-8Wdev9FmPdhx03j5o95rz4xvtmtTIUW5qUH7Jww

If we want to get the file information in the folder, we need to use the following endpoint.
Test Endpoint:

https://graph.microsoft.com/v1.0/drives/{driveId}/root:/{folderPath}:/children

For example,

https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drives/b!7Hw7W_7Lk0imOMGKNMajlK0n-8Wdev9FmPdhx03j5o95rz4xvtmtTIUW5qUH7Jww/root:/Business%20Central:/children

Here we can get the file (item) id.
File (item) id: 01AA2EHNP46T7UJIJ6DZBYKJUOMSKBYWRZ

Finally, let’s read the contents of the file, 1988-S.jpg.

Test Endpoint:

https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}/content

For example,

https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drive/items/01AA2EHNP46T7UJIJ6DZBYKJUOMSKBYWRZ/content

Let’s do one more test this time, try uploading a file to SharePoint from Postman. Here is the endpoint I tested (Including content).

PUT

https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{folder}/{filename}:/content

For example, I want to upload the SharePointUploadTest.csv file to Documents (root) -> Business Central folder.

Endpoint

https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drives/b!7Hw7W_7Lk0imOMGKNMajlK0n-8Wdev9FmPdhx03j5o95rz4xvtmtTIUW5qUH7Jww/root:/Business Central/SharePointUploadTest.csv:/content

Then the http action is not Get, but Put.
Headers -> Content-Type -> text/csv:

Body -> binary -> upload file:

Choose Send.
201 Created: A new resource was created successfully.

If the specified folder is not found, a new folder will be created automatically.

Test video:

Very simple. More details: Working with SharePoint sites in Microsoft Graph and Upload or replace the contents of a driveItem

PS:
1. If you get the following error, please check the API permissions in App Registration.

        “code”: “AccessDenied”,
        “message”: “Either scp or roles claim need to be present in the token.”,

2. If you encounter a 401 Unauthorized error, please check whether the Scope is set to the graph API. (https://graph.microsoft.com/.default)

        “code”: “InvalidAuthenticationToken”,
        “message”: “Access token validation failure. Invalid audience.”,

3. If you use delegated permissions, use the following endpoint:

https://graph.microsoft.com/v1.0/me/drive/root/

4. When the file cannot be found, the following error message will be displayed:

        “code”: “itemNotFound”,
        “message”: “The resource could not be found.”

5. When opening the file in Postman, because it can only be opened in XML, HTML, Txt or Json format, the following situation may occur, but the test is successful.

OK, next let’s see if we can upload files from AL. Here are a few important points.
Get OAuth Token:

Using OAuth Token to upload file to SharePoint: Http action – Put in AL (If a file with the same name already exists, it will be overwritten)

This time I did two simple tests in total, one is manually uploaded, and the other is automatically uploaded through Job Queue. I hope it can be of some help to everyone.

1. Generate reports for selected posted sales invoices and upload to SharePoint

PS: We need to generate reports, which may not have a fixed layout and report. More details: Dynamics 365 Business Central: Bulk export/download reports with different layouts to a zip file (Download Posted Sales Invoices/Posted Purchase Invoices to a zip file)

Test:

Test video:

Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50115 PostedSalesInvoicesExt extends "Posted Sales Invoices"
{
    actions
    {
        addafter("&Invoice")
        {
            action(GenerateReportsAndUploadToSharePoint)
            {
                ApplicationArea = All;
                Caption = 'Generate Reports And Upload To SharePoint';
                Image = Import;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    SharePointHandler: Codeunit SharePointHandler;
                    SalesInvHeader: Record "Sales Invoice Header";
                    i: Integer;
                begin
                    i := 0;
                    SalesInvHeader.Reset();
                    CurrPage.SetSelectionFilter(SalesInvHeader);
                    if SalesInvHeader.FindSet() then
                        repeat
                            SharePointHandler.UploadFilesToSharePoint(SalesInvHeader);
                            i += 1;
                        until SalesInvHeader.Next() = 0;
                    if i > 0 then
                        Message('%1 files uploaded to SharePoint successfully.', i);
                end;
            }
        }
    }
}
codeunit 50120 SharePointHandler
{
    procedure UploadFilesToSharePoint(SalesInvHeader: Record "Sales Invoice Header")
    var
        HttpClient: HttpClient;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
        Headers: HttpHeaders;
        ContentHeader: HttpHeaders;
        RequestContent: HttpContent;
        JsonResponse: JsonObject;
        AuthToken: SecretText;
        SharePointFileUrl: Text;
        ResponseText: Text;
        FileContent: InStream;
        TempBlob: Codeunit "Temp Blob";
        FileName: Text;
        MimeType: Text;
        SalesInvHeader2: Record "Sales Invoice Header";
        ReportSelection: Record "Report Selections";
        TempReportSelections: Record "Report Selections" temporary;
    begin
        // Get OAuth token
        AuthToken := GetOAuthToken();
        if AuthToken.IsEmpty() then
            Error('Failed to obtain access token.');

        // Generate the report and save it as a PDF file
        SalesInvHeader2.Get(SalesInvHeader."No.");
        SalesInvHeader2.SetRecFilter();
        ReportSelection.FindReportUsageForCust(Enum::"Report Selection Usage"::"S.Invoice", SalesInvHeader2."Bill-to Customer No.", TempReportSelections);
        Clear(TempBlob);
        TempReportSelections.SaveReportAsPDFInTempBlob(TempBlob, TempReportSelections."Report ID", SalesInvHeader2, TempReportSelections."Custom Report Layout Code", Enum::"Report Selection Usage"::"S.Invoice");
        TempBlob.CreateInStream(FileContent);
        FileName := Format(SalesInvHeader2."No." + '.pdf');
        MimeType := 'application/pdf';

        // Define the SharePoint folder URL
        // application permissions (replace with the actual site-id, drive-id, folder path and file name)
        SharePointFileUrl := 'https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drives/b!7Hw7W_7Lk0imOMGKNMajlK0n-8Wdev9FmPdhx03j5o95rz4xvtmtTIUW5qUH7Jww/root:/Business Central/' + FileName + ':/content';
        // Initialize the HTTP request
        HttpRequestMessage.SetRequestUri(SharePointFileUrl);
        HttpRequestMessage.Method := 'PUT';
        HttpRequestMessage.GetHeaders(Headers);
        Headers.Add('Authorization', SecretStrSubstNo('Bearer %1', AuthToken));
        RequestContent.GetHeaders(ContentHeader);
        ContentHeader.Clear();
        ContentHeader.Add('Content-Type', MimeType);
        HttpRequestMessage.Content.WriteFrom(FileContent);

        // Send the HTTP request
        if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
            // Log the status code for debugging
            //Message('HTTP Status Code: %1', HttpResponseMessage.HttpStatusCode());
            if HttpResponseMessage.IsSuccessStatusCode() then begin
                //HttpResponseMessage.Content.ReadAs(ResponseText);
                //JsonResponse.ReadFrom(ResponseText);
                //Message(ResponseText);
            end else begin
                //Report errors!
                HttpResponseMessage.Content.ReadAs(ResponseText);
                Error('Failed to upload files to SharePoint: %1 %2', HttpResponseMessage.HttpStatusCode(), ResponseText);
            end;
        end else
            Error('Failed to send HTTP request to SharePoint');
    end;

    procedure GetOAuthToken() AuthToken: SecretText
    var
        ClientID: Text;
        ClientSecret: Text;
        TenantID: Text;
        AccessTokenURL: Text;
        OAuth2: Codeunit OAuth2;
        Scopes: List of [Text];
    begin
        ClientID := 'b4fe1687-f1ab-4bfa-b494-0e2236ed50bd';
        ClientSecret := 'huL8Q~edsQZ4pwyxka3f7.WUkoKNcPuqlOXv0bww';
        TenantID := '7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62';
        AccessTokenURL := 'https://login.microsoftonline.com/' + TenantID + '/oauth2/v2.0/token';
        Scopes.Add('https://graph.microsoft.com/.default');
        if not OAuth2.AcquireTokenWithClientCredentials(ClientID, ClientSecret, AccessTokenURL, '', Scopes, AuthToken) then
            Error('Failed to get access token from response\%1', GetLastErrorText());
    end;
}

2. Use Job Queue to periodically upload invoices for the current date to SharePoint

If the above method passes the test, the second one is relatively simple. You can create a trigger OnRun() in Codeunit. For example, upload the invoices with today’s posting date to SharePoint.

Then you can set it in the Job Queue Entries, for example, to run once every night.

Test video:

Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)

codeunit 50120 SharePointHandler
{
    trigger OnRun()
    var
        SalesInvHeader: Record "Sales Invoice Header";
    begin
        SalesInvHeader.Reset();
        SalesInvHeader.SetRange("Posting Date", Today);
        if SalesInvHeader.FindSet() then
            repeat
                UploadFilesToSharePoint(SalesInvHeader);
            until SalesInvHeader.Next() = 0;
    end;

    procedure UploadFilesToSharePoint(SalesInvHeader: Record "Sales Invoice Header")
    var
        HttpClient: HttpClient;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
        Headers: HttpHeaders;
        ContentHeader: HttpHeaders;
        RequestContent: HttpContent;
        JsonResponse: JsonObject;
        AuthToken: SecretText;
        SharePointFileUrl: Text;
        ResponseText: Text;
        FileContent: InStream;
        TempBlob: Codeunit "Temp Blob";
        FileName: Text;
        MimeType: Text;
        SalesInvHeader2: Record "Sales Invoice Header";
        ReportSelection: Record "Report Selections";
        TempReportSelections: Record "Report Selections" temporary;
    begin
        // Get OAuth token
        AuthToken := GetOAuthToken();
        if AuthToken.IsEmpty() then
            Error('Failed to obtain access token.');

        // Generate the report and save it as a PDF file
        SalesInvHeader2.Get(SalesInvHeader."No.");
        SalesInvHeader2.SetRecFilter();
        ReportSelection.FindReportUsageForCust(Enum::"Report Selection Usage"::"S.Invoice", SalesInvHeader2."Bill-to Customer No.", TempReportSelections);
        Clear(TempBlob);
        TempReportSelections.SaveReportAsPDFInTempBlob(TempBlob, TempReportSelections."Report ID", SalesInvHeader2, TempReportSelections."Custom Report Layout Code", Enum::"Report Selection Usage"::"S.Invoice");
        TempBlob.CreateInStream(FileContent);
        FileName := Format(SalesInvHeader2."No." + '.pdf');
        MimeType := 'application/pdf';

        // Define the SharePoint folder URL
        // application permissions (replace with the actual site-id, drive-id, folder path and file name)
        SharePointFileUrl := 'https://graph.microsoft.com/v1.0/sites/5b3b7cec-cbfe-4893-a638-c18a34c6a394/drives/b!7Hw7W_7Lk0imOMGKNMajlK0n-8Wdev9FmPdhx03j5o95rz4xvtmtTIUW5qUH7Jww/root:/Business Central/' + FileName + ':/content';
        // Initialize the HTTP request
        HttpRequestMessage.SetRequestUri(SharePointFileUrl);
        HttpRequestMessage.Method := 'PUT';
        HttpRequestMessage.GetHeaders(Headers);
        Headers.Add('Authorization', SecretStrSubstNo('Bearer %1', AuthToken));
        RequestContent.GetHeaders(ContentHeader);
        ContentHeader.Clear();
        ContentHeader.Add('Content-Type', MimeType);
        HttpRequestMessage.Content.WriteFrom(FileContent);

        // Send the HTTP request
        if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
            // Log the status code for debugging
            //Message('HTTP Status Code: %1', HttpResponseMessage.HttpStatusCode());
            if HttpResponseMessage.IsSuccessStatusCode() then begin
                //HttpResponseMessage.Content.ReadAs(ResponseText);
                //JsonResponse.ReadFrom(ResponseText);
                //Message(ResponseText);
            end else begin
                //Report errors!
                HttpResponseMessage.Content.ReadAs(ResponseText);
                Error('Failed to upload files to SharePoint: %1 %2', HttpResponseMessage.HttpStatusCode(), ResponseText);
            end;
        end else
            Error('Failed to send HTTP request to SharePoint');
    end;

    procedure GetOAuthToken() AuthToken: SecretText
    var
        ClientID: Text;
        ClientSecret: Text;
        TenantID: Text;
        AccessTokenURL: Text;
        OAuth2: Codeunit OAuth2;
        Scopes: List of [Text];
    begin
        ClientID := 'b4fe1687-f1ab-4bfa-b494-0e2236ed50bd';
        ClientSecret := 'huL8Q~edsQZ4pwyxka3f7.WUkoKNcPuqlOXv0bww';
        TenantID := '7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62';
        AccessTokenURL := 'https://login.microsoftonline.com/' + TenantID + '/oauth2/v2.0/token';
        Scopes.Add('https://graph.microsoft.com/.default');
        if not OAuth2.AcquireTokenWithClientCredentials(ClientID, ClientSecret, AccessTokenURL, '', Scopes, AuthToken) then
            Error('Failed to get access token from response\%1', GetLastErrorText());
    end;
}

Very simple, give it a try!!!😁

PS:
1. Dynamics 365 Business Central: How to import/read files from SharePoint (document library) to Business Central via AL (Graph API)

2. Dynamics 365 Business Central: How to upload files from Business Central to SharePoint (document library) via AL (Graph API)

3. Dynamics 365 Business Central: Preview files within BC (PDF, JPG, PPTX, DOCX, XLSX, TXT, CSV, MP4…… Most file types supported for previewing files in SharePoint) – No JavaScript control add-in

4. Dynamics 365 Business Central: How to save reports directly to OneDrive via AL (Graph API) – Manual and Automatic

END

Hope this will help.

Thanks.

ZHU

コメント

Copied title and URL