Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : h82WP+ufvXpjQzDcVB4vOk
Small and medium business | Business Central, N...
Answered

Document attachment web service

Like (3) ShareShare
ReportReport
Posted on 31 Jan 2025 20:52:53 by 95
Hi all,
We've integrated customized portal with BC
1. Which codeunit should I use such that if I send the document to BC with attachment from portal, the same attachment will be attached automatically in BC's integrated document.
2. Secondly if I want to do the vice versa i.e. reading data from BC to portal, which codeunit should I call to get the attachment from BC to portal?
  • Verified answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 8,786 Super User 2025 Season 1 on 03 Feb 2025 at 06:36:55
  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 44,619 on 03 Feb 2025 at 03:02:44
    Document attachment web service
    In BC, the Document Attachment Table is used to save files associated with specific records. This process typically employs a Blob as the parameter. However, if the request originates from the portal, a stream format is required instead. To address this, you'll need to create a custom function that converts the stream to a Blob format. Ultimately, the goal is to ensure that a record is created in the Document Attachment Table. Be sure to review the functions available within that table for guidance.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,925 Super User 2025 Season 1 on 03 Feb 2025 at 00:51:12
    Document attachment web service
  • Suggested answer
    Saif Ali Sabri Profile Picture
    Saif Ali Sabri 760 Super User 2025 Season 1 on 01 Feb 2025 at 05:19:32
    Document attachment web service

    AI was used to create this answer
    To handle document attachments between a customized portal and Microsoft Dynamics 365 Business Central (BC), you can use the following approaches:


    1. Sending Document Attachments from Portal to BC:

    To automatically attach documents sent from the portal to BC, you can use the Document Attachment table and its related codeunits.

    • Table: Document Attachment (Table ID: 1173)

    • Codeunit: Document Attachment Management (Codeunit ID: 1175)

    Steps:

    1. Prepare the Attachment:

      • Convert the document (e.g., PDF, Word, etc.) into a base64-encoded string or byte array.

      • Ensure you have the necessary metadata (e.g., document name, file extension, related record ID).

    2. Call the Codeunit:

      • Use the Document Attachment Management codeunit to create and attach the document to the relevant record in BC.

      • Example AL code snippet:

        al
        Copy
        procedure AttachDocumentToRecord(RecordId: RecordId; FileName: Text; FileExtension: Text; FileContent: Text)
        var
            DocumentAttachment: Record "Document Attachment";
            DocumentAttachmentManagement: Codeunit "Document Attachment Management";
            InStream: InStream;
            TempBlob: Codeunit "Temp Blob";
        begin
            // Convert base64 content to a stream
            TempBlob.FromBase64String(FileContent);
            TempBlob.CreateInStream(InStream);
        
            // Create and attach the document
            DocumentAttachment.Init();
            DocumentAttachment."Table ID" := RecordId.TableNo;
            DocumentAttachment."No." := RecordId.RecordKey;
            DocumentAttachment."File Name" := FileName;
            DocumentAttachment."File Extension" := FileExtension;
            DocumentAttachment."Document Reference ID".ImportStream(InStream, FileName);
            DocumentAttachment.Insert(true);
        
            // Optionally, validate the attachment
            DocumentAttachmentManagement.ValidateAttachment(DocumentAttachment);
        end;
    3. Expose the Codeunit as a Web Service:

      • Publish the codeunit as a web service in BC so that the portal can call it.


    2. Reading Document Attachments from BC to Portal:

    To retrieve attachments from BC and display them on the portal, you can use the same Document Attachment table and codeunit.

    Steps:

    1. Fetch Attachments:

      • Query the Document Attachment table to retrieve attachments linked to a specific record.

      • Example AL code snippet:

        al
        Copy
        procedure GetDocumentAttachments(RecordId: RecordId; var DocumentAttachments: List of [Text])
        var
            DocumentAttachment: Record "Document Attachment";
            TempBlob: Codeunit "Temp Blob";
            OutStream: OutStream;
            Base64Content: Text;
        begin
            DocumentAttachment.SetRange("Table ID", RecordId.TableNo);
            DocumentAttachment.SetRange("No.", RecordId.RecordKey);
            if DocumentAttachment.FindSet() then
                repeat
                    TempBlob.CreateOutStream(OutStream);
                    DocumentAttachment."Document Reference ID".ExportStream(OutStream);
                    Base64Content := TempBlob.ToBase64String();
                    DocumentAttachments.Add(Base64Content);
                until DocumentAttachment.Next() = 0;
        end;
    2. Expose the Codeunit as a Web Service:

      • Publish the codeunit as a web service to allow the portal to call it and retrieve the attachments.

    3. Portal Integration:

      • Call the BC web service from the portal to fetch the attachments.

      • Decode the base64 content and display or download the document on the portal.


    Key Considerations:

    • Permissions: Ensure the portal has the necessary permissions to access BC web services.

    • File Size: Handle large files efficiently by chunking or compressing them if needed.

    • Error Handling: Implement robust error handling for failed uploads or downloads.

    • Security: Use secure communication (HTTPS) and validate user inputs to prevent injection attacks.

    By using the Document Attachment table and Document Attachment Management codeunit, you can seamlessly integrate document attachments between your portal and BC.

  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 8,786 Super User 2025 Season 1 on 01 Feb 2025 at 04:52:06

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,313 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans