SetSaveStream - Document Attachment

NwMibNwMib Member Posts: 1
I'm trying to add a Document Attachment record via an Unchase Connected Service to a Microsoft Dynamics NAV database in C# (Visual Studio 2019).
I downloaded the XML metadata from a Dynamics NAV URL. In Visual Studio, I perform an Add / Update Unchase Connected Service and point it at the metadata file. This process auto-generates a Reference.cs code-file. I then call the appropriate objects / properties / methods (see code below):-

const int SHTABLEID = 5900;
string lstrFileName = @C:\Test\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);
var ms = new MemoryStream();
System.Guid lobjGuid = System.Guid.NewGuid();
lobjDA.DocumentReferenceID = NAV.Media.CreateMedia(lobjGuid);

gobjDSC_Bell.SetSaveStream(lobjDA.DocumentReferenceID.Content, ms, true, new DataServiceRequestArgs { ContentType = "application/pdf" }); // ERROR


The SetSaveStream line of code above produces the error: 'Value cannot be null. Parameter name: entity' Sure enough the Content property is null.
As the Content property is of type: DataServiceStreamLink and there doesn't appear to be a way of instantiating it in the conventional way
(e.g.) lobjDA.DocumentReferenceID.Content = new DataServiceStreamLink();
error CS1729: 'DataServiceStreamLink' does not contain a constructor that takes 0 arguments

The hierarchy of objects used is shown below:
lobjDA : NAV.API_Document_Attachment
lobjDA.DocumentReferenceID : NAV.Media
lobjDA.DocumentReferenceID.Content : DataServiceStreamLink

… I'm stuck in a bit of a stalemate at the moment. Any thoughts? Thanks in advance for any support given.


  • krikikriki Member, Moderator Posts: 9,061
    [Topic moved from 'NAV Tips & Tricks' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!

  • walemarkwalemark Member Posts: 1
    When you provided a constructor for your class that takes arguments, the compiler no longer creates an empty constructor. Therefore, you cannot call an empty constructor because it does not exist. You would need to explicitly write the constructor that takes 0 arguments in your class's code. The constructor of the inheritance class needs to construct the base class first. since the base class does not have a default constructor (taking 0 arguments) and you are not using the non-default constructor you have now, this won't work. so either:
    • Add a default constructor to your base class, in which case the code of the descending class needs no change;

    • Call the non-default constructor of the base class from the constructor of the descending class, in which case the base class needs no change.
Sign In or Register to comment.