Connect to SFTP Server from D365 FO X++

 To connect to the SFTP server we required Renci.SSHnet dll should be available in our instanse. We can follow the below process to create the dll.

1. Open Visual studio 2019, then create a C# library project.

2.  Then right click on the project and click on Manage NuGet packages.


3. Now Click on the settings button appears right corner of the window. Then check for package Nuget.org. If it is not available you can create a new package with same name and add following link in the source.
 



Note: The package should be activated to select that in the next step.

4. Now select package and navigate to browse section enter Renci in search box and install below two packages.




5. Now your project will be looks like this




6. Add one class and create the below methods to establish the connection to SFTP.

using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SFTPConnection
{
    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;
        }
    }
}
with the below code we can connect to the SFTP using SSH private key
public  SftpClient OpenSFTPConnection(string host,
                         string username,
                         string password,
                         System.IO.Stream sourceFile,
                         string destinationPath,
                         int port,
                         string fileName,
                         string privateKeyFilePath = "")
 {
     string successStr = "Fail";
     List<AuthenticationMethod> methods;

     /*It depends if the private key file is present for authentication. 
     If the SFTP is key secured then the private key file has to be passed.*/
     if (privateKeyFilePath != "")
     {
         var privateKeyFile = new PrivateKeyFile(privateKeyFilePath);// passPhrase - Password for key file
         methods = new List<AuthenticationMethod>
         {
             new PasswordAuthenticationMethod(username, password),
             new PrivateKeyAuthenticationMethod(username, privateKeyFile)
         };
     }
     else
     {
         methods = new List<AuthenticationMethod>
         {
             new PasswordAuthenticationMethod(username, password)
         };
     }

     try
     {
         var connectionInfo = new ConnectionInfo(host, port, username, methods.ToArray());

         using (SftpClient sftpclient = new SftpClient(connectionInfo))
         {
             if (this.sftpClient == null)
             {
                 this.sftpClient = sftpclient;
             }
             if (!this.sftpClient.IsConnected)
             {
                 this.sftpClient.Connect();
             }
         }

         successStr = "Pass";
     }
     catch (WebException e)
     {
         Console.WriteLine($"Error: {e.Message}");
     }

     return this.sftpClient;
 }

7. Now Build your project, the required DLL will be generated in bin folder of your project.
8. Now copy the Renci Dll and newly created SFTPConnection dll and paste that in your model bin folder.
9. Add the Dll to your FNO project reference by using add reference feature.
10. Now you can use the reference and establish connection to SFTP by using X++ code.

In additional, you can see how to connect to the FTP server in my previous blog here.

Thank you !!















No comments:

Post a Comment