Let’s have a look at the ways for browsing and displaying the files using the new web interface of Dynamics 365. I will give you a couple of different options to send the files to the user, and later mention about the new document management in Dynamics 365 and a way to preview the files you have stored inside a form.

We create a simple form to display the stored file data in a grid, using the table from our previous blog as datasource. We add two buttons to our form, one to download the stored file (or view it using browser plugins), another button to attach the file as a file attachment to the record itself.

capture001

capture002

Since we have used XML files in our example, we can display it to the user readable as is. Remember we can also store any kind of binary file as blob data in our database. The file we upload does not have to be string file. We can use .NET stream classes with any kind of binary data.

Since we currently use a string type of file as an example, our first method to download (or display) our file is the following straightforward method :

[Control("Button")]
class Download
{
public void clicked()
{
super();</pre>
if(TestXMLTable.xml)
{
//1. method
File::SendStringAsFileToUser(TestXMLTable.xml, 'xmlfile.xml', System.Text.Encoding::UTF8, classstr (FileUploadTemporaryStorageStrategy));

}}}}

This simple code line saves the string data as a file in temporary storage and sends it as a downloaded file to the user. Then user can download it as a file or display it on his own machine. This method only works for string values stored in the database and if we want to use binary values instead, then we need to use .NET stream classes and another method in File class to send the stream to the user:

[Control("Button")]
 class Download
 {
 public void clicked()
 {
 super();
 if(TestXMLTable.xml)
 {
 using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
using(System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
{

writer.Write(TestXMLTable.xml);
writer.Flush();
stream.Position = 0;

File::SendFileToUser(stream, 'xmlfile.xml', classstr (FileUploadTemporaryStorageStrategy));
}
 }}}}

This method saves the .NET stream into a file using selected storage strategy and sends it to user as a downloadable file. Now let’s assume we would like to display the stored document to user in a new tab instead of sending it as a downloadable file. In our example; to replace the old AIFXMLViewer form in AX 2012 with a XML displayer on a new tab. I use a chrome browser for the example below with its plugin to display XML files which are opened in the tab (Install and use a real chrome browser, not the chrome add-in which comes with VS and displayed when you debug and run your project; Disable popup blocker if you have any).

[Control("Button")]
 class Download
 {
 public void clicked()
 {
 super();
 if(TestXMLTable.xml)
 {
//3. method
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
using(System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
{
str url;

writer.Write(TestXMLTable.xml);
writer.Flush();
stream.Position = 0;

url = File::SendFileToTempStore(stream, 'xmlfile.xml', classstr (FileUploadTemporaryStorageStrategy));

new Browser().navigate(url, true);
}
 }}}}

Here in this method, we send our file to temp store ourselves and browse it using the navigate function :

capture003

File attachment with Document Management

The new document management in Dynamics 365 can use .NET streams just like the rest of the methods we have shown here. You can upload and attach a file to a record loading the file into.NET streams. There is also a possibility to get a public URL of your attached file. In the example here, I did not setup a sharepoint site nor others to publicize the documents but will show you the method to get the public url.

[Control("Button")]
class AttachAsFile
{
public void clicked()
{
super();
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
using(System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
{
str url;
DocuRef docuref;

writer.Write(TestXMLTable.xml);
writer.Flush();
stream.Position = 0;

docuref = DocumentManagement::attachFileToCommon(TestXMLTable, DocuType::typeFile(), stream, 'xmlfile.xml', 'XML', 'xmlfile.xml');

URL = DocumentManagement::getAttachmentPublicUrl(docuref);

info(URL);
}
}

}

Now we can see our stored file as an attachment to our record in Dynamics Document Management by clicking the attachment icon on the top right hand side :

capture004

Notice the preview tab which provides a dynamic document preview depending on the file type we attached. You can use the same if you like to display your stored database files to the user on your form. For now, I will keep my example simple and will not get into such detail.

The last method we use in the code return us a public url for the file we attached. In my example it gave me such an url that you cannot browse into. I think the return url becomes a browseable one if you do certain settings to publicize your attachments in Document Management system, perhaps with setting up a sharepoint connection. Please be welcomed to send your comments if you know how this return value becomes a browseable url.

capture006

You can download the test project used in this blog from GitHub link below:

TestFileInteraction.axpp

 

[adinserter block=”9″]

5 thoughts on “How to work with files in AX 7 Part 2 – Browse and download files stored in DB, attach files to the new Document Management

  1. Hi,

    thanks for your great work. The solution is quite simple for the public url. Just add your AX basepath like https://…….cloudax.dynamics.com/ + url

    best regards,

    Alexander

  2. Very great post !
    Which control did you use in Attachment form to visualize properly the xml ?
    And how did you set it

    Thanks
    Dona

    1. Thanks, Unfortunately no control does that in AX7, you just download the XML file to open, use chrome to display it on browser window, or like me install a Chrome XML Viewer plugin to display it after it is downloaded, that are the options.

Leave a Reply