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())
{
    lobjFS.CopyTo(ms);
    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
}
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 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)
Related
Recommended