web
You’re offline. This is a read only version of the page.
close
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 891

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?

I have the same question (0)
  • Community Member Profile Picture
    on at

    Do you know which NAV object you called ?

  • Nick Webb Profile Picture
    891 on at

    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
    on at

    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
    891 on at

    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
    on at

    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
    891 on at

    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?

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

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 3,151

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,443 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,092 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans