Sending Flat Files from Dynamics 365 for Operations to a Different Location

By Bill Thompson | February 28, 2017

Sending flat files from dynamics 365 for operations to a different location

One of the challenges we have found with Dynamics 365 for Operations is the export of flat files. Recently we had to get data (as a flat file) out of Dynamics 365 for Operations and place it in a location for a separate system to process. The process had to be secure.

Initially, we were going to use the .NET Framework functionality for FTP-ing the data to an FTP site, but found that out of the box .NET does not provide the secure FTP functionality. There are many different options (some freeware, some paid). This discussion will implement a freeware version using SSH.NET (

The first step is to build the dll. Once built, a reference needs to be added to the Visual Studio project. One issue we found is that this reference needs to be in a location that will be available on all different environments (Development, UAT, and Production). We ended up copying the dll (Renci.SshNet.dll) to the AOS file structure, and placing it in the root of our Model (see screenshot below).

Flat files from Dynamics 365

This file was added to the Visual Studio project, and then added into version control. This allows the file to follow the project throughout the promotion of code.

The code below is part of a class that was created to allow the batching of the file upload process (which is called AFTER the file has been created and validated to exist). The code logs into the FTP site, and once successful uploads the file to the defined location.

All of the configuration information is stored in a custom table called SysFTPSiteInfo.

Below is the code for the ftp method used. Error trapping is NOT implemented in this demonstration code.

/// <summary>
    /// This method implements sftp functionality
    /// </summary>
    /// <remarks>
    /// </remarks>
    public void ftpFile()
        str ftpAddress;
        str ftpLocation;
        str ftpUser;
        str ftpPW;

        SysFTPSiteInfo ftpInfo;

        System.Object ftpo;
        System.Object ftpResponse;
        System.Net.FtpWebRequest request;
        System.IO.StreamReader reader;
        System.IO.Stream requestStream;
        System.Byte[] bytes;
        System.Text.Encoding utf8;
        System.Net.FtpWebResponse response;
        System.Object credential;
        Filename        fileType,fileNameString;
        FileName        ftpFileName,fileName;

        new InteropPermission(InteropKind::ClrInterop).assert();

        IntFilePathTable pathFile;
        // find the location where to save the file
        select firstonly pathFile;
        filePath = pathFile.FilePath;

        // get the ftp site info
        select firstonly ftpInfo;
        ftpAddress = ftpInfo.FTPSite;
        ftpLocation = ftpInfo.FTPFolder;
        ftpUser = ftpInfo.FTPUserId;


        // decrypt the password 
        ftpPW = cryptography.decrypt(ftpInfo.FTPPassword);        

        fileName localfileName = filePath + @'\' + expFileName;

        [filepath, filename, fileType] = fileNameSplit(localfileName);
        fileNameString= expFileName;
        ftpFileName = ftpAddress + '/' + ftpLocation + '/' + fileNameString;

        reader = new System.IO.StreamReader(localfileName);
        utf8 = System.Text.Encoding::get_UTF8();
        bytes = utf8.GetBytes( reader.ReadToEnd() );

        // secure file transfer via SFTP
        using (var sftp = new Renci.SshNet.SftpClient(ftpAddress,ftpUser,ftpPW))
            sftp.ChangeDirectory(@"/" + ftpLocation);

            using (var fileStream = new System.IO.FileStream(localfileName, System.IO.FileMode::Open))
                sftp.BufferSize = 4 * 1024;



Related Posts

Recommended Reading:

Manage U.S. Use Tax on Purchase Orders in Dynamics 365 Finance and Operations

  Managing sales tax requirements on your business purchase can be complicated, but Dynamics 365 Finance and Operations can help […]

Read the Article
5.19.22 Dynamics CRM

How to Write a Great Support Ticket in the Stoneridge Support Portal

Submitting a support ticket through the Stoneridge Support Portal is a quick and effective way to get assistance for any […]

Read the Article

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article
5.5.22 Dynamics GP

The Real Story about the Long-Term Future of Dynamics GP Support

I’ve seen a number of people put forward comment that Dynamics GP is going away and you have to get […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Financial Enhancements

The Dynamics 365 Businses Central 2022 Wave 1 Release has a lot of new and exciting features to help your […]

Read the Article
4.29.22 Dynamics GP

Dynamics GP Transaction Removals: Bank Reconciliation

  This is part 2 of a 3 part series on Dynamics GP Transaction Removals. These quick tips will hopefully […]

Read the Article

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!