Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

How do I extract pdf attachment through API

Posted on by 57

I am trying to extract pdf attachment from business central through a custom api

I created an API page for the attachment document table but the pdf documents are not accessible.

Categories:
  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: How do I extract pdf attachment through API

    hi

    Create a custom API page for the Attachment table. In the API page, you should include the fields you need to retrieve the PDF attachment, such as Attachment ID, Attachment Name, and Attachment Data.

    In the API page, add a codeunit that retrieves the Attachment Data field for the PDF attachment. You can use the "Attachment Management" codeunit to do this.

    Modify the "OnGetRecord" trigger in the API page to call the codeunit that retrieves the Attachment Data field. For example, you can add the following code to the "OnGetRecord" trigger:

    Trigger OnGetRecord()

    Var

       AttachmentMgt: Codeunit "Attachment Management";

    Begin

       AttachmentMgt.GetAttachmentStream(Attachment.ID, Attachment."File Name", PDFStream);

    End;

    In the above code, "AttachmentMgt" is the codeunit variable that references the "Attachment Management" codeunit. "PDFStream" is a variable that holds the stream of the PDF attachment.

    Return the PDF attachment as the response of the API call. You can use the "SendStream" function to do this. For example, you can add the following code to the "OnAfterGetRecord" trigger:

    Trigger OnAfterGetRecord()

    Begin

       Response.ContentDisposition := 'attachment;filename="' + Attachment."File Name" + '"';

       Response.ContentType := 'application/pdf';

       Response.SendStream(PDFStream);

    End;

    In the above code, "Response" is the response variable that is returned by the API page. "ContentDisposition" and "ContentType" are the HTTP headers that specify the file name and content type of the PDF attachment. "SendStream" sends the PDF stream as the response of the API call.

    DAniele

  • Suggested answer
    Bilal Haider Profile Picture
    Bilal Haider 45 on at
    RE: How do I extract pdf attachment through API

    Hi,

    You can convert pdf to base64 text and send the base64 text to endpoint.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans