Posted in AX 365

Read File’s from SFTP Server and Write data in AX365 (PART 1)

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..

Author:

An ERP Expert who can design , develop ,integrate and implement cost effective solutions using Dynamics ERP at the core and surrounding Microsoft Technology stack. Customer focused and has solid experience in building Service oriented frameworks for Dynamics AX ERP and custom built legacy apps, Expert in building BI Reports with SSRS and SSAS Understands the project life-cycle and development of ERP solutions using the product development processes and methodologies proposed by Microsoft. SQL Performance: Moderate in Troubleshooting Performance issues with SQL Technical Summary: Specialties: Microsoft Dynamics AX 2009 and 2012 design and development Dynamics AX 2009, 2012, AX7 Installation and Configuration Dynamics AX 2012,2009 Product development Dynamics AX 2012, 2009 Development and customization Dynamics AX 2012 R3 Warehouse mobile portal customization Dynamics AX 2012 EP Development and customization Dynamics AX 2012 Data Migration Framework Dynamics AX 2012 SSRS development Tolls and Languages I worked on are, X++, C#, AX Reporting, SSRS reports, EP development (and configuration) DIXF , IDMF, Integration of outlook with AX Skype Id : wajjii2 Email address: wajahatali_1991@hotmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s