Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Document Attachment

(0) ShareShare
ReportReport
Posted on by 703

I'm trying to add a Document Attachment record via an OData Connected Service to a Microsoft Dynamics NAV database in C# (Visual Studio Professional 2017), (see code below):-

string lstrFileName = @"C:\Users\user.name\Documents\PDF\BCTest\Test Attachment 3.pdf";
System.IO.FileInfo lfi = new System.IO.FileInfo(lstrFileName);
NAV.API_Document_Attachment lobjDA = NAV.API_Document_Attachment.CreateAPI_Document_Attachment(5900, "SO000006", "Order", 0, 1);
lobjDA.AttachedDate = DateTime.Now;
lobjDA.FileName = lfi.Name;
lobjDA.FileExtension = lfi.Extension.Replace(".", string.Empty);
lobjDA.FileType = "2";
objContext.AddToAPI_Document_Attachment(lobjDA);
objContext.SaveChanges();
… but when it gets to SaveChanges, I get: "{"error":{"code":"Application_DialogException","message":"Please attach a document first."}}"

How do I programmatically attach a document?

  • Nick Webb Profile Picture
    Nick Webb 703 on at
    RE: Document Attachment

    My code now is:-

    string lstrFileName = @"C:\somefolder\filetoattach.pdf";

    System.IO.FileInfo lfi = new System.IO.FileInfo(lstrFileName);

    NAV.API_Document_Attachment lobjDA = NAV.API_Document_Attachment.CreateAPI_Document_Attachment(5900, "SO000006", "Order", 0, 1);

    lobjDA.AttachedDate = DateTime.Now;

    lobjDA.FileName = lfi.Name;

    lobjDA.FileExtension = lfi.Extension.Replace(".", string.Empty);

    lobjDA.FileType = "2";

    FileStream lobjFS = new FileStream(lstrFileName, FileMode.Open);

    byte[] larrFileBytes = null;

    using (var ms = new MemoryStream())

    {

       lobjFS.CopyTo(ms);

       larrFileBytes = ms.ToArray();

       System.Guid lobjGuid = System.Guid.NewGuid();

       lobjDA.DocumentReferenceID = NAV.Media.CreateMedia(lobjGuid);

       // lobjDA.DocumentReferenceID.Content = ms;

    }

    gobjDSC_Bell.AddToAPI_Document_Attachment(lobjDA);

    gobjDSC_Bell.SaveChanges();

    The bit I think I'm now missing is, how to populate the Content property (type: Microsoft.OData.Client.DataServiceStreamLink) of the DocumentReferenceID (type: NAV.Media)

    I'm still getting error: 'Please attach a document first.' at SaveChanges.

    Any help / sample source code would be appreciated?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Document Attachment

    I don't have in C#. I use Power Automate to attached the document to BC API.

    You should convert it into base64 then assign the value to the field

  • Nick Webb Profile Picture
    Nick Webb 703 on at
    RE: Document Attachment

    Interesting. Thanks for the tip. So I've included the Document Reference ID column in the API Page and re-ingested the metadata and it sees this column as a Media type property / field in C#. So, do you have any sample code of streaming the file to a field of this type?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Document Attachment

    There is a field Document Reference ID in table Document Attachment. You need to stream the file to that field first before you can insert record to document attachment

  • Nick Webb Profile Picture
    Nick Webb 703 on at
    RE: Document Attachment

    So, I created an API Page in an AL Extension with SourceTable = "Document Attachment". I then deployed the extension, wrapped a Web Service round it, downloaded the metadata into a Connected Service in Visual Studio and tried to run the code above. Is that enough info?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Document Attachment

    Do you know which NAV object you called ?

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,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans