Document Attachment via OData

I’m trying to add a Document Attachment record via a Connected Service to a Microsoft Dynamics NAV database in C# (Visual Studio Professional 2019), (see code below):-
const int SHTABLEID = 5900;
string lstrFileName = @“C:\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(SHTABLEID, “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())
larrFileBytes = ms.ToArray();
System.Guid lobjGuid = System.Guid.NewGuid();
lobjDA.DocumentReferenceID = NAV.Media.CreateMedia(lobjGuid);

// I think some code is required here to populate lobjDA.DocumentReferenceID.Content

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 getting error: ‘Please attach a document first.’ at SaveChanges.How do I attach the document / file?

NAV details below:-
Version: Platform 14.0.29530.0 + Application 29537 (GB Dynamics NAV 14.0)

Did you ever figure out the solution? I am running into something similar. Using a NAV OData source in a Visual Studio project. Employed the Unchase OData Connected Service extension and I cannot assign the Content attributes to the new picture I’m trying to POST.

If memory serves, I never found a solution. I had to go with an unsatisfactory workaround which was to store the attachment on cloud storage and save a URL link in BC to it.

1 Like