top of page
Writer's pictureTabrez Ajaz

Read/Download any website content from URL in Dynamics 365 Business Central

Updated: Feb 21, 2022

Welcome Dynamics 365 BC Lovers,


In this article I will explain how to read/download any website HTML content from a given URL and store it in a file, but if you want to store the content in any Business Central table Blob field you can refer my another post in this post I already explained how to read/write in Blob fields. To achieve this I will create a simple standard dialog page with 2 fields and a simple procedure CreateFile to create a file.


Demonstration Video:


Standard Dialog Page – Code Snippet: Used TempBlob record is not available in the later version of BC14 or in the Online environment, so if you are not using BC14 then please make sure to use TempBlob as Codeunit datatype, not as a record.

page 80027 ReadAndDownloadWebContent
{
    Caption = 'Read/Download Any Website Content - Demo';
    PageType = StandardDialog;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field("File Name"; gFileName)
                {
                    ApplicationArea = All;
                }
                field(URL; URL)
                {
                    ApplicationArea = All;

                    trigger OnValidate()
                    var
                        httpClient: HttpClient;
                        httpResponseMsg: HttpResponseMessage;
                        websiteContent: Text;
                        FileName: Text;
                    begin
                        if gFileName = '' then
                            Error('Please Enter Filename first!');
                        // Check internet connectivity
                        if not httpClient.Get(URL, httpResponseMsg) then
                            Message('Make sure to check your internet connectivity!')
                        else begin
                            httpResponseMsg.Content.ReadAs(websiteContent);
                            if websiteContent <> '' then begin
                                if CreateFile(websiteContent) then
                                    Message('Thank you for downloading html content from: ' + URL);
                                Message('Your website Content: ' + websiteContent);
                            end;
                        end;
                    end;
                }
            }
        }
    }

    procedure CreateFile(content: Text): Boolean
    var
        inStr: InStream;
        outStr: OutStream;
        tempBlob: Record TempBlob temporary;
        fileName: Text;
    begin
        // Create the outstream
        fileName := gFileName;
        tempBlob.Blob.CreateOutStream(outStr, TextEncoding::Windows);

        // Wrtie to the text file
        outStr.WriteText(content);

        // Create instream and download to the user
        tempBlob.Blob.CreateInStream(inStr, TextEncoding::Windows);
        DownloadFromStream(inStr, '', '', '', fileName);
        exit(true);
    end;



    var
        URL: Text; // Accept Website URL
        gFileName: Text; // File name to download
}
 

I hope you understand how to download any website HTML content in Dynamics 365 Business Central.


Stay Tuned!

41 views0 comments

Comments


bottom of page