web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

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

Document attachment web service

(2) ShareShare
ReportReport
Posted on by 173
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?
I have the same question (0)
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    21,048 Super User 2025 Season 2 on at
  • Suggested answer
    Saif Ali Sabri Profile Picture
    2,352 Super User 2025 Season 2 on at

    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
    YUN ZHU Profile Picture
    96,039 Super User 2025 Season 2 on at
  • Suggested answer
    Suresh Kulla Profile Picture
    50,251 Super User 2025 Season 2 on at
    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.
  • Verified answer
    Khushbu Rajvi. Profile Picture
    21,048 Super User 2025 Season 2 on at

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,651

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 808 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 718 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans