Recently I got a requirement to read files from SFTP directory and write in AX. There were many challenges I faced. I will try to cover all in this post.
In Part 1, I will Create a project with basic SFTP functionality which we will use in AX in Part 2
Basic Step is to add the Parameters to set the SFTP details.

Tips:
If we are using Port 22 then no need to use a prefix of SFTP in your Host because 21 is for FTP and 22 is for SFTP
Your File path should start from the directory root folder not from the SFTP server name.
To Connect to the SFTP Server. I used SSH.NET Dll.
Create a New C# Library project, Right Click on your project ,Click on Manage NUGET Packages and Install ssh.net In your project. It will add a reference of DLL into your project.

Add a reference of this dll in your class
using Renci.SshNet;
Now we need to write a method to make a connection,
public SftpClient OpenSFTPConnection(string host, int port, string username, string password) { if (this.sftpClient == null) { this.sftpClient = new Renci.SshNet.SftpClient(host, port, username, password); } if (!this.sftpClient.IsConnected) { this.sftpClient.Connect(); } return this.sftpClient; }
This method will return all the files present in the directory
public List<string> GetDirectories(SftpClient _SftpClient, string path) { return _SftpClient.ListDirectory(path)/*.Where(n => n.IsDirectory).ToList(); }
This method will return the stream which we can use to read the content of the files.
public Stream DownloadFile(SftpClient _SftpClient, string sourcePath) { var memoryStream = new MemoryStream(); _SftpClient.DownloadFile(sourcePath, memoryStream); memoryStream.Position = 0; return memoryStream; }
I also got a requirement to move files to different folder after writing data in AX. Below method will do that magic.
public void MoveFile(SftpClient _SftpClient, string sourcePath, string destinationPath, bool isPosix) { _SftpClient.RenameFile(sourcePath, destinationPath, isPosix); }
Final Code for our library project will look like this
using Renci.SshNet; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MySshNet { public class sftpConnection { public SftpClient sftpClient; public SftpClient OpenSFTPConnection(string host, int port, string username, string password) { if (this.sftpClient == null) { this.sftpClient = new Renci.SshNet.SftpClient(host, port, username, password); } if (!this.sftpClient.IsConnected) { this.sftpClient.Connect(); } return this.sftpClient; } public List<string> GetDirectories(SftpClient _SftpClient, string path) { return _SftpClient.ListDirectory(path)/*.Where(n => n.IsDirectory)*/.Select(n => n.Name).ToList(); } public void MoveFile(SftpClient _SftpClient, string sourcePath, string destinationPath, bool isPosix) { _SftpClient.RenameFile(sourcePath, destinationPath, isPosix); } public Stream DownloadFile(SftpClient _SftpClient, string sourcePath) { var memoryStream = new MemoryStream(); _SftpClient.DownloadFile(sourcePath, memoryStream); memoryStream.Position = 0; return memoryStream; } } }
Now Just build your project then go to your projects bin\Debug folder and copy both Dll’s. paste the DLL’s to your AX extension Bin Folder so we can use it in our AX project.


Tips:
We need to add our Dll’s in our extension Bin folder so It can go through with check in to the next environments.
Now we are ready to Consume the Dll’s in Our AX project in Part 2..