Transferring files over FTP & SFTP protocol

This post is about transferring the file over FTP (File Transfer Protocol) & SFTP (Secured File Transfer Protocol) from Navision using WinSCP dot net library. Files can be uploaded to & downloaded from remote system.

Below is the design that i have implemented in one of our projects.

  1. FTP Setup – to configure the connection details.
  2. FTP Setup Line – to configure the methods like upload, download and file paths.
  3. FTP Log

FTP Setups:

FTP_Setup_List

FTP Setup:

You can create multiple FTP Setup for different connections.

  1. Active field can be used to Activate / Deactivate the FTP Setup.
  2. Sequence field can be used to setup the sequence in which the FTP batch job can process the FTP Setup.
  3. Connection can be tested using Test Connection.

FTP_Setup_1

FTP Setup Line:

  1. Active field can be used to Activate / Deactivate individual FTP Setup Line.
  2. Line level Sequence field can be used to setup the sequence in which Setup Line can be processed.
  3. Uploaded files can be archived & deleted using the “Archive & Delete” option.

FTP_Setup_2

Batch Job for Processing the FTP Setup.

Connection is only opened once for each & every Active FTP Setup and only Active Lines are processed based on the sequence defined in the setup.

FTP_Batch_Job

FTP Logs are created when processing the the FTP Setup . Log contains the details about the files that are Uploaded & Downloaded, connection & transfer related error’s .

FTP_Log

Batch job can be scheduled in Job Queue:

JobQueue_

Sample code can be found from  WinSCP

WinSCP Library can be downloaded from here  and the files should be copied to the Add-ins folder.

SFTP can be tested with Rebex Tiny SFT Server .

Please do let me know your suggestion on the design and share this post. Thanks

Advertisements

10 thoughts on “Transferring files over FTP & SFTP protocol

  1. Hi Divesh, Your Implementation is good.This is what we are looking for.

    Please share the objects if possible ,it would be very helpful for NAV team in saving time.

    Like

  2. Hi,
    the implemantation looks quit well but without the code from the FTPManagement called from the report this is only the concept and not the solution. Can you please share your code from the expected codeunit.

    Thanks,
    chrischbo

    Like

  3. Hi Diveshbora,

    the implementation is good, now my question is can I do this on the Automation datatype instead of DotNet? Your reply will be much appreciated 🙂

    Thanks,
    Marc

    Like

  4. Hi Diveshbora,
    I have done something very similar, I am not sure if you got my other message?
    I have it all except the code that sets/returns the WinSCP Remote Exception, collection or lines, any chance you could email me and share a couple of lines of code to return the fault messages.

    Kind Regards

    David

    Like

    • Hi David,

      Below function CheckForTransferError will help you to get the remote exception, which can be invoked after the below lines of the code for PutFiles / GetFiles.

      WinSCPTransferOpResult := WinSCPSession.PutFiles
      CheckForTransferError();

      WinSCPTransferOpResult := WinSCPSession.GetFiles
      CheckForTransferError();

      local procedure CheckForTransferError()
      var
      lEnum: DotNet IEnumerator;
      lString: DotNet String;
      lErrorMsg: Text;
      lWinSCPSessionRemoteExpCol: DotNet SessionRemoteExceptionCollection;
      begin
      //CheckForTransferError
      IF NOT WinSCPTransferOpResult.IsSuccess() THEN BEGIN
      lWinSCPSessionRemoteExpCol := WinSCPTransferOpResult.Failures();
      lEnum := lWinSCPSessionRemoteExpCol.GetEnumerator();
      WHILE lEnum.MoveNext() DO BEGIN
      lString := lEnum.Current();
      lErrorMsg += lString.ToString() + ‘\’;
      END;
      ERROR(lErrorMsg);
      END;
      end;

      Hope this helps. Thanks

      Best Regards,
      Divesh

      Like

      • Hi Divesh,
        I am already using similar code, I just had a deeper look, the IsSucess is set to true when there are no failures, to me I expect there could be success and failures in the same session, if I use a file mask trasactions*.xml and the first ‘transactions’ file in the mask can be downloaded but the second transactions file cannot, lets say it because it is locked, that would give a success and a failure in the same session.

        It was more for when there is a complete failure to connect to the remote ftp site I was after, however in testing you cannot get an IF return for WinSCPSesh.Open(WinSCPSessionOptions); so it just causes a System Error, So I have had to call a second codeunit CLEARLASTERROR; and GETERRORTEXT to log the failure.

        I see your log and the Error “The call to WinSCP Session Open failed” was what I was looking at, but I assume like me you are using the System Error functions and not something in WinSCP.

        Many Thanks

        David

        Like

      • Hi David,

        I have used Codeunit.Run for Older versions and TryFunctions for the New Versions of NAV to handle the unexpected errors from dot net library. I think the IsSuccess is false if there is any error when performing the operation, so this should work as defined in the link https://winscp.net/eng/docs/library_transferoperationresult.

        Also we can try this below code, which will throw any error
        // Throw on any error
        transferResult.Check();

        https://winscp.net/eng/docs/library_session_getfiles
        https://winscp.net/eng/docs/library_session_putfiles

        Best Regards,
        Divesh

        Like

Leave a Reply to Marc Anthony Lugo Cancel 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