Transfer Nav Exported file to SFTP directory file and download file from SFTP to Nav directory.

ManiNavManiNav Member Posts: 120
Hi Everyone,[Nav 2017]

I would like to transfer Nav Exported file to SFTP directory file and download file from SFTP to Nav directory.(File is in Csv format)

Eg. Nav folder: C:\Desktop\DEMO\ForSFTP
> SFTP folder: C:\Desktop\DEMO\FromNav
Nav folder: C:\Desktop\DEMO\FromSFTP <
SFTP folder: C:\Desktop\DEMO\ForNav

For this I am using WinSCP. And, I want to do this with codeunit. How can i proceed this. Please help about Cal\code.

thanks,
Mani
«1

Comments

  • ftorneroftornero Member Posts: 522
    Hello @ManiNav ,

    This is an example to download files from and SFTP site
    SFTP_GetFilesList(PortNumber : Integer;FTPAddress : Text;Login : Text;Password : Text;remotePath : Text;localPath : Text;fileMask : Text;logFile : Text) : Integer
    WinSCP_SessionOptions := WinSCP_SessionOptions.SessionOptions;
    WinSCP_SessionOptions.Protocol := WinSCP_Protocol.Sftp;
    IF PortNumber <= 0 THEN
      WinSCP_SessionOptions.PortNumber := 22
    ELSE
      WinSCP_SessionOptions.PortNumber := PortNumber;
    WinSCP_SessionOptions.HostName := FTPAddress;
    WinSCP_SessionOptions.Password := Password; 
    WinSCP_SessionOptions.UserName := Login;
    WinSCP_SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE; 
    
    WinSCP_Session := WinSCP_Session.Session;
    WinSCP_Session.SessionLogPath(logFile);
    WinSCP_Session.Open(WinSCP_SessionOptions);
    
    WinSCP_TransferOptions := WinSCP_TransferOptions.TransferOptions;
    IF fileMask <> '' THEN
      WinSCP_TransferOptions.FileMask(fileMask);
    WinSCP_TransferResult := WinSCP_Session.GetFiles(remotePath, localPath, FALSE, WinSCP_TransferOptions);
    WinSCP_TransferResult.Check();
    WinSCP_Transfers := WinSCP_TransferResult.Transfers;
    EXIT(WinSCP_Transfers.Count);
    

    Where the vars are:
    Name	DataType	Subtype	Length
    WinSCP_Protocol	DotNet	WinSCP.Protocol.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    WinSCP_Session	DotNet	WinSCP.Session.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    WinSCP_SessionOptions	DotNet	WinSCP.SessionOptions.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    WinSCP_TransferOptions	DotNet	WinSCP.TransferOptions.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    WinSCP_TransferResult	DotNet	WinSCP.TransferOperationResult.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    WinSCP_Transfers	DotNet	WinSCP.TransferEventArgsCollection.'WinSCPnet, Version=1.5.5.8565, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'	
    

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Thanks for the sample cal/code.
    few things, i want to clear about parameter that you use in the above function:
    remotePath(Text) = SFTP Path ??
    localPath(Text) = Local path directory ??
    fileMask(Text) = not confirm
    logFile(Text) = not confirm

    please guide me. Can i transfer file from SFTP file to Nav directory only running by codeunit ie the above code will work for codeunit. OR, have to create job queue like that to run?

    Please help me.

    Thanks,
    Mani.

  • ftorneroftornero Member Posts: 522
    Hello @ManiNav

    I reply in your quote:
    ManiNav wrote: »
    Hi ftornero,

    Thanks for the sample cal/code.
    few things, i want to clear about parameter that you use in the above function:
    remotePath(Text) = SFTP Path ??
    > Yes, it is the SFTP path
    localPath(Text) = Local path directory ?? ---> It is your local path but you must to decide if you call this code running on client or on server to deal with this path
    fileMask(Text) = not confirm
    > It is the mask that you can use to the files i.e. "*.jpg"
    logFile(Text) = not confirm
    > It is the file where WinSCP write what is doing. It apply the same that already said to the localPath.

    please guide me. Can i transfer file from SFTP file to Nav directory only running by codeunit ie the above code will work for codeunit. OR, have to create job queue like that to run?

    The code is a function that you can include in a codeunit or a report and you can call directly or inside the job queue.

    Please help me.

    Thanks,
    Mani.

    Regards

  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    I am trying the below codeunit, but I am not able to get the file from SFTP directory to My Nav local directory. Please check and guide me,what and where i am doing mistake.

    OnRun()

    remotePath :='/<root>/in'; //SFTP path,from where i want to get file
    localPath :='C:\Desktop\3PL DEMO\FromSFTP'; // local path,where i want to save
    fileMask := '*.Csv'; //format of file
    logFile := 'C:\Program Files (x86)\WinSCP'; // WinSCP installed path


    SFTP_GetFilesList(PortNumber : Integer;FTPAddress : Text;Login : Text;Password : Text;remotePath : Text;localPath : Text;fileMask : Text;logFile : Text) : Integer


    WinSCP_SessionOptions := WinSCP_SessionOptions.SessionOptions;
    WinSCP_SessionOptions.Protocol := WinSCP_Protocol.Sftp;
    IF PortNumber <= 0 THEN
    WinSCP_SessionOptions.PortNumber := 22
    ELSE
    WinSCP_SessionOptions.PortNumber := 8743;
    WinSCP_SessionOptions.HostName := gsdhs.co;
    WinSCP_SessionOptions.Password := 873ewdd@3;
    WinSCP_SessionOptions.UserName := abc;
    WinSCP_SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE;

    WinSCP_Session := WinSCP_Session.Session;
    WinSCP_Session.SessionLogPath(logFile);
    WinSCP_Session.Open(WinSCP_SessionOptions);

    WinSCP_TransferOptions := WinSCP_TransferOptions.TransferOptions;
    IF fileMask <> '' THEN
    WinSCP_TransferOptions.FileMask(fileMask);
    WinSCP_TransferResult := WinSCP_Session.GetFiles(remotePath, localPath, FALSE, WinSCP_TransferOptions);
    WinSCP_TransferResult.Check();
    WinSCP_Transfers := WinSCP_TransferResult.Transfers;
    EXIT(WinSCP_Transfers.Count);

    Thanks,
    Mani.
  • ManiNavManiNav Member Posts: 120
    Hi Everyone/ftornero,

    I tried the below code also, but I am not able to get the file from SFTP to nav directory. Could you please check and suggest/guide me regarding my issue.

    OnRun()

    SourceFileP :='C:\3PL DEMO\FromSFTP';
    RemotPath :='/<root>/in';


    LOCAL UploadFile(SourceFileP : Text)

    WinSCPSessionOptions := WinSCPSessionOptions.SessionOptions();
    WinSCPSessionOptions.Protocol := WinSCP_Protocol.Sftp;
    WinSCPSessionOptions.HostName := 'sftpxm.co';
    WinSCPSessionOptions.PortNumber := 4322;
    WinSCPSessionOptions.UserName := 'abcd';
    WinSCPSessionOptions.Password := '9843fueeiwei223';
    WinSCPSessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE;
    WinSCPSesh := WinSCPSesh.Session();
    WinSCPSesh.ExecutablePath(WinScp_Executable_Path);
    WinSCPSesh.Open(WinSCPSessionOptions);
    IF WinSCPSesh.Opened THEN BEGIN
    WinSCPTransferOptions := WinSCPTransferOptions.TransferOptions;
    WinSCPTransferOptions.TransferMode := 0;
    WinSCPTransferResumeSupport := WinSCPTransferOptions.ResumeSupport;
    WinSCPTransferResumeSupport.State(WinSCPTransferResumeSupportState.Off);

    IF WinSCPSesh.FileExists(RemotPath + 'upload/' + FileManagement.GetFileName(SourceFileP)) THEN
    WinSCPSesh.RemoveFiles(RemotPath + 'upload/' + FileManagement.GetFileName(SourceFileP));
    WinSCPTransferResults := WinSCPSesh.PutFiles(SourceFileP, RemotPath + 'uploads/',FALSE,WinSCPTransferOptions);
    END ELSE ERROR('Connecion Failed!');
    IF WinSCPSesh.Opened THEN WinSCPSesh.Dispose();


    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav ,

    Lets focus in your first example
    ManiNav wrote: »

    OnRun()

    remotePath :='/<root>/in'; //SFTP path,from where i want to get file
    localPath :='C:\Desktop\3PL DEMO\FromSFTP'; // local path,where i want to save
    fileMask := '*.Csv'; //format of file
    logFile := 'C:\Program Files (x86)\WinSCP'; // WinSCP installed path

    It's the remote path correct ? -> '/<root>/in'

    The logFile must be a file not a folder, try this one :
    logFile := 'C:\Desktop\3PL DEMO\FromSFTP\logFile.txt';
    

    After running the code you could be the content of this file to see what has happend.

    And the more important thing is how you declare the dotNET variables, by default are declared to run on Server, so the local paths and files are actually in the server where the service is running not in your client computer.

    eub7pfunwol4.png

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    According to your suggestion, i put the 1.logFile:
    logFile := 'C:\Desktop\3PL DEMO\FromSFTP\logFile.txt'; And I tried for .Csv format also.

    2. I corrrect the remote path(/in) also, from where the file will take(.txt/.Csv).
    But, I am not getting my file in destination(Local directry/path)
    So, Please guide me how can i pass the source file path(remote path) in my function, because it seems like its not calling that path in my sourceFile function.
    Your help will be appreciable.
    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav ,

    Could you send what is in the logFIle after running your code ?

    And if you can export your code in txt format and send it too, better.

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    After running code, I am not getting anything in logfile. its showing blank.
    For checking purpose I created one text/Csv file with inside some text, so that after running codeunit that text should come in my local path file from remot path directory. But its not coming.

    I put here content of exported codeunit in txt format for reference[I am not able to attach the exported txt file here].Please check and guide me.
    Thank you so much for your continuous help :) .



    OBJECT Codeunit 50120 SFTPmanagement_M
    {
    OBJECT-PROPERTIES
    {
    Date=10/22/19;
    Time=11:51:08 AM;
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    OnRun=BEGIN
    remotePath :='/in';
    localPath :='C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP';
    fileMask := '*.Csv';
    //logFile := 'C:\Program Files (x86)\WinSCP';
    logFile := 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP\logFile.Csv';
    END;

    }
    CODE
    {
    VAR
    WinSCP_SessionOptions@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.SessionOptions";
    WinSCP_Protocol@1000000001 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Protocol";
    WinSCP_Session@1000000002 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Session";
    WinSCP_TransferOptions@1000000003 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOptions";
    WinSCP_TransferResult@1000000004 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOperationResult";
    WinSCP_Transfers@1000000005 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferEventArgsCollection";
    remotePath@1000000006 : Text;
    localPath@1000000007 : Text;
    fileMask@1000000008 : Text;
    logFile@1000000009 : Text;

    PROCEDURE SFTP_GetFilesList@1000000001(PortNumber@1000000000 : Integer;FTPAddress@1000000001 : Text;Login@1000000002 : Text;Password@1000000003 : Text;remotePath@1000000004 : Text;localPath@1000000005 : Text;fileMask@1000000006 : Text;logFile@1000000007 : Text) : Integer;
    BEGIN
    MESSAGE('SFTPTrigger');
    WinSCP_SessionOptions := WinSCP_SessionOptions.SessionOptions;
    WinSCP_SessionOptions.Protocol := WinSCP_Protocol.Sftp;
    IF PortNumber <= 0 THEN
    WinSCP_SessionOptions.PortNumber := 22
    ELSE
    WinSCP_SessionOptions.PortNumber := 7362;
    WinSCP_SessionOptions.HostName := 'sftp.co';
    WinSCP_SessionOptions.Password := '37etwtas2WE';
    WinSCP_SessionOptions.UserName := 'abcjsd';
    WinSCP_SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE;

    WinSCP_Session := WinSCP_Session.Session;
    WinSCP_Session.SessionLogPath(logFile);
    WinSCP_Session.Open(WinSCP_SessionOptions);

    WinSCP_TransferOptions := WinSCP_TransferOptions.TransferOptions;
    IF fileMask <> '' THEN
    WinSCP_TransferOptions.FileMask(fileMask);
    WinSCP_TransferResult := WinSCP_Session.GetFiles(remotePath, localPath, FALSE, WinSCP_TransferOptions);
    WinSCP_TransferResult.Check();
    WinSCP_Transfers := WinSCP_TransferResult.Transfers;
    EXIT(WinSCP_Transfers.Count);
    END;

    EVENT WinSCP_Session@1000000002::FileTransferred@49(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000002::Failed@50(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.FailedEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000002::OutputDataReceived@51(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.OutputDataReceivedEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000002::FileTransferProgress@52(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.FileTransferProgressEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000002::QueryReceived@53(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.QueryReceivedEventArgs");
    BEGIN
    END;

    BEGIN
    END.
    }
    }


    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav

    If you are running this codeunit you are missing the call to the function SFTP_GetFileList.

    After
    logFile := 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP\logFile.Csv';
    

    And better with a txt extension not a csv one, because is a text file.

    You must call the function:
    SFTP_GetFilesList(0, 'sftp.co', 'abcjsd', '37etwtas2WE', remotePath, localPath, fileMask, logFile);
    

    And change this part of your code to get the parameters
    IF PortNumber <= 0 THEN
      WinSCP_SessionOptions.PortNumber := 22
    ELSE
      WinSCP_SessionOptions.PortNumber := 7362;
    WinSCP_SessionOptions.HostName := 'sftp.co';
    WinSCP_SessionOptions.Password := '37etwtas2WE';
    WinSCP_SessionOptions.UserName := 'abcjsd';
    

    And like I said before, your dotNet variables are defined to run on the server so the paths and the files that you are dealing with are in the server not in your local machine.

    Regards
  • ftorneroftornero Member Posts: 522
    Something like this
    OBJECT Codeunit 50120 SFTPmanagement_M
    {
      OBJECT-PROPERTIES
      {
        Date=10/22/19;
        Time=11:34:00 AM;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                remotePath :='/in';
                localPath :='C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP';
                fileMask := '*.Csv';
                //logFile := 'C:\Program Files (x86)\WinSCP';
                logFile := 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP\logFile.txt';
                SFTP_GetFilesList(7362, 'sftp.co', 'abcjsd', '37etwtas2WE', remotePath, localPath, fileMask, logFile);
              END;
    
      }
      CODE
      {
        VAR
          WinSCP_SessionOptions@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.SessionOptions";
          WinSCP_Protocol@1000000001 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Protocol";
          WinSCP_Session@1000000002 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Session";
          WinSCP_TransferOptions@1000000003 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOptions";
          WinSCP_TransferResult@1000000004 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOperationResult";
          WinSCP_Transfers@1000000005 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferEventArgsCollection";
          remotePath@1000000006 : Text;
          localPath@1000000007 : Text;
          fileMask@1000000008 : Text;
          logFile@1000000009 : Text;
    
        PROCEDURE SFTP_GetFilesList@1000000001(PortNumber@1000000000 : Integer;FTPAddress@1000000001 : Text;Login@1000000002 : Text;Password@1000000003 : Text;remotePath@1000000004 : Text;localPath@1000000005 : Text;fileMask@1000000006 : Text;logFile@1000000007 : Text) : Integer;
        BEGIN
          //MESSAGE('SFTPTrigger');
          WinSCP_SessionOptions := WinSCP_SessionOptions.SessionOptions;
          WinSCP_SessionOptions.Protocol := WinSCP_Protocol.Sftp;
          IF PortNumber <= 0 THEN
            WinSCP_SessionOptions.PortNumber := 22
          ELSE
            WinSCP_SessionOptions.PortNumber := PortNumber;
          WinSCP_SessionOptions.HostName := FTPAddress;
          WinSCP_SessionOptions.Password := Password;
          WinSCP_SessionOptions.UserName := Login;
          WinSCP_SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE;
    
          WinSCP_Session := WinSCP_Session.Session;
          WinSCP_Session.SessionLogPath(logFile);
          WinSCP_Session.Open(WinSCP_SessionOptions);
    
          WinSCP_TransferOptions := WinSCP_TransferOptions.TransferOptions;
          IF fileMask <> '' THEN
            WinSCP_TransferOptions.FileMask(fileMask);
          WinSCP_TransferResult := WinSCP_Session.GetFiles(remotePath, localPath, FALSE, WinSCP_TransferOptions);
          WinSCP_TransferResult.Check();
          WinSCP_Transfers := WinSCP_TransferResult.Transfers;
          EXIT(WinSCP_Transfers.Count);
        END;
    
        BEGIN
        END.
      }
    }
    
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    I put the path and file both in server. And i tried the same code as you provide above. But, I am getting the below error, when i run codeunit{Its trigger the error at line:
    WinSCP_Session.Open(WinSCP_SessionOptions); }.


    Microsoft Dynamics NAV

    A call to WinSCP.Session.Open failed with this message: Error occurred during logging. It's been turned off.
    Can't open log file 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\FromSFTP\logFile.txt'.
    System Error. Code: 5.
    Access is denied
    OK

    5a5yj1ulyf3i.png

    Could you please, guide me; where i am doing wrong.

    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav,

    Seems like it is a permission issue, change all the dotNET variables to run on client and try again.

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Thank you so much. Now, I am able to export file from remote(SFTP) directory to local path.
    Next part: I am trying to export from local path to remote(SFTP) directory.
    As it will complete,will update you.
    ONCE AGAIN THANKS FOR YOUR HELP. :)

    Thanks,
    Mani.
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Same codeunit, i am trying to upload from local path to SFTP remote directory. Only, I changed the lineA with lineB. the codeunit is runing successfuly, but file is not transferring from local to remote directory.
    [I put the local path file alredy in server]

    lineA: WinSCP_TransferResult := WinSCP_Session.GetFiles(localPath,remotePath, FALSE,WinSCP_TransferOptions);//Remove

    lineB: WinSCP_TransferResult := WinSCP_Session.PutFiles(localPath,remotePath, FALSE, WinSCP_TransferOptions); //Insert

    Please check and guide me further.

    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav,

    It must works, could you send the code in text format again?

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Thank you so muchhhhhhhh............
    Below is the content of my codeunit.


    OBJECT Codeunit 50121 LocalTOSFTP_M
    {
    OBJECT-PROPERTIES
    {
    Date=10/23/19;
    Time=[ 6:22:53 PM];
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    OnRun=BEGIN
    remotePath :='/in';
    localPath :='C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP';
    fileMask := '*.Csv';
    logFile := 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\LogFileForSFTP\logFile.txt';
    SFTP_GetFilesList(7362, 'sftp.co', 'abcjsd', '37etwtas2WE', remotePath, localPath, fileMask, logFile);
    END;

    }
    CODE
    {
    VAR
    WinSCP_SessionOptions@1000000009 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.SessionOptions" RUNONCLIENT;
    WinSCP_Protocol@1000000008 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Protocol";
    WinSCP_Session@1000000007 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Session" RUNONCLIENT;
    WinSCP_TransferOptions@1000000006 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOptions" RUNONCLIENT;
    WinSCP_TransferResult@1000000005 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOperationResult" RUNONCLIENT;
    WinSCP_Transfers@1000000004 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferEventArgsCollection" RUNONCLIENT;
    remotePath@1000000003 : Text;
    localPath@1000000002 : Text;
    fileMask@1000000001 : Text;
    logFile@1000000000 : Text;

    PROCEDURE SFTP_GetFilesList@1000000001(PortNumber@1000000000 : Integer;FTPAddress@1000000001 : Text;Login@1000000002 : Text;Password@1000000003 : Text;remotePath@1000000004 : Text;localPath@1000000005 : Text;fileMask@1000000006 : Text;logFile@1000000007 : Text) : Integer;
    BEGIN
    WinSCP_SessionOptions := WinSCP_SessionOptions.SessionOptions;
    WinSCP_SessionOptions.Protocol := WinSCP_Protocol.Sftp;
    IF PortNumber <= 0 THEN
    WinSCP_SessionOptions.PortNumber := 22
    ELSE
    WinSCP_SessionOptions.PortNumber := PortNumber;
    WinSCP_SessionOptions.HostName := FTPAddress;
    WinSCP_SessionOptions.Password := Password;
    WinSCP_SessionOptions.UserName := Login;
    WinSCP_SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := TRUE;

    WinSCP_Session := WinSCP_Session.Session;
    WinSCP_Session.SessionLogPath(logFile);
    WinSCP_Session.Open(WinSCP_SessionOptions);
    WinSCP_TransferOptions := WinSCP_TransferOptions.TransferOptions;
    IF fileMask <> '' THEN
    WinSCP_TransferOptions.FileMask(fileMask);
    WinSCP_TransferResult := WinSCP_Session.PutFiles(localPath,remotePath, FALSE, WinSCP_TransferOptions);
    WinSCP_TransferResult.Check();
    WinSCP_Transfers := WinSCP_TransferResult.Transfers;
    EXIT(WinSCP_Transfers.Count);
    END;

    EVENT WinSCP_Session@1000000007::FileTransferred@49(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000007::Failed@50(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.FailedEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000007::OutputDataReceived@51(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.OutputDataReceivedEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000007::FileTransferProgress@52(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.FileTransferProgressEventArgs");
    BEGIN
    END;

    EVENT WinSCP_Session@1000000007::QueryReceived@53(sender@1000000001 : Variant;e@1000000000 : DotNet "'WinSCPnet, Version=1.6.5.9849, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.QueryReceivedEventArgs");
    BEGIN
    END;

    BEGIN
    END.
    }
    }

    Please check and guide me.

    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav,

    The code looks ok,

    Could you send the logfile created after the execution of this code ?.

    Regards.
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Below is the content of log file after running the codeunit.


    . 2019-10-23 18:58:22.907
    . 2019-10-23 18:58:22.908 WinSCP Version 5.15.4 (Build 9849) (OS 10.0.14393 - Windows Server 2016 Standard)
    . 2019-10-23 18:58:22.908 Configuration: nul
    . 2019-10-23 18:58:22.908 Log level: Normal
    . 2019-10-23 18:58:22.908 Local account: KTC\maniratnamkumarm
    . 2019-10-23 18:58:22.908 Working directory: C:\Program Files (x86)\WinSCP
    . 2019-10-23 18:58:22.908 Process ID: 11548
    . 2019-10-23 18:58:22.909 Command-line: "C:\Program Files (x86)\WinSCP\winscp.exe" /xmllog="C:\Users\maniratnamkumarm\AppData\Local\Temp\9\wscp1BC4.0282F632.tmp" /xmlgroups /xmllogrequired /nointeractiveinput /dotnet=5.15.4 /ini=nul /log="C:\Users\maniratnamkumarm\Desktop\3PL DEMO\LogFileForSFTP\logFile.txt" /console /consoleinstance=_7108_43689923_734
    . 2019-10-23 18:58:22.909 Time zone: Current: GMT+5:30 (Sri Lanka Standard Time), No DST
    . 2019-10-23 18:58:22.909 Login time: Wednesday, October 23, 2019 6:58:22 PM
    . 2019-10-23 18:58:22.909
    . 2019-10-23 18:58:22.909 Script: Retrospectively logging previous script records:
    > 2019-10-23 18:58:22.909 Script: option batch on
    < 2019-10-23 18:58:22.909 Script: batch on
    < 2019-10-23 18:58:22.909 Script: reconnecttime 120
    > 2019-10-23 18:58:22.909 Script: option confirm off
    < 2019-10-23 18:58:22.909 Script: confirm off
    > 2019-10-23 18:58:22.909 Script: option reconnecttime 120
    < 2019-10-23 18:58:22.909 Script: reconnecttime 120
    > 2019-10-23 18:58:22.909 Script: open sftp://(My/Sftp username):***@(My host name):(My port no) -hostkey="*" -timeout=15
    . 2019-10-23 18:58:22.909
    . 2019-10-23 18:58:22.910 Session name: (My/Sftp username)@(My host name) (Ad-Hoc site)
    . 2019-10-23 18:58:22.910 Host name: (My host name) (Port: (My port no))
    . 2019-10-23 18:58:22.910 User name: (My/Sftp username) (Password: Yes, Key file: No, Passphrase: No)
    . 2019-10-23 18:58:22.910 Tunnel: No
    . 2019-10-23 18:58:22.910 Transfer Protocol: SFTP
    . 2019-10-23 18:58:22.910 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
    . 2019-10-23 18:58:22.910 Disable Nagle: No
    . 2019-10-23 18:58:22.910 Proxy: None
    . 2019-10-23 18:58:22.910 Send buffer: 262144
    . 2019-10-23 18:58:22.910 SSH protocol version: 2; Compression: No
    . 2019-10-23 18:58:22.910 Bypass authentication: No
    . 2019-10-23 18:58:22.910 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
    . 2019-10-23 18:58:22.910 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom:
    . 2019-10-23 18:58:22.910 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
    . 2019-10-23 18:58:22.910 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
    . 2019-10-23 18:58:22.910 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
    . 2019-10-23 18:58:22.910 Simple channel: Yes
    . 2019-10-23 18:58:22.910 Return code variable: Autodetect; Lookup user groups: Auto
    . 2019-10-23 18:58:22.910 Shell: default
    . 2019-10-23 18:58:22.910 EOL: LF, UTF: Auto
    . 2019-10-23 18:58:22.910 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
    . 2019-10-23 18:58:22.910 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
    . 2019-10-23 18:58:22.910 SFTP Bugs: Auto,Auto
    . 2019-10-23 18:58:22.910 SFTP Server: default
    . 2019-10-23 18:58:22.910 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
    . 2019-10-23 18:58:22.911 Cache directory changes: Yes, Permanent: Yes
    . 2019-10-23 18:58:22.911 Recycle bin: Delete to: No, Overwritten to: No, Bin path:
    . 2019-10-23 18:58:22.911 DST mode: Unix
    . 2019-10-23 18:58:22.911
    . 2019-10-23 18:58:22.911 Looking up host "(My host name)" for SSH connection
    . 2019-10-23 18:58:22.917 Connecting to 35.241.217.254 port (My port no)
    . 2019-10-23 18:58:23.150 We claim version: SSH-2.0-WinSCP_release_5.15.4
    . 2019-10-23 18:58:23.388 Server version: SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u7
    . 2019-10-23 18:58:23.388 Using SSH protocol version 2
    . 2019-10-23 18:58:23.621 Doing ECDH key exchange with curve Curve25519 and hash SHA-256
    . 2019-10-23 18:58:24.851 Server also has ssh-rsa host key, but we don't know it
    . 2019-10-23 18:58:24.854 Host key fingerprint is:
    . 2019-10-23 18:58:24.854 ssh-ed25519 256 d5:6c:40:ea:dc:ff:58:1e:70:eb:4a:82:74:b4:f0:93



    The remaining part is in next comment.
  • ManiNavManiNav Member Posts: 120
    P8plFGP3ZC7IwboWCdpT94TSnXi4mypY7fDmMAtSq1M=
    * 2019-10-23 18:58:24.855 WARNING! Giving up security and accepting any host key as configured!
    . 2019-10-23 18:58:24.855 Initialised AES-256 SDCTR client->server encryption
    . 2019-10-23 18:58:24.855 Initialised HMAC-SHA-256 client->server MAC algorithm
    . 2019-10-23 18:58:24.855 Initialised AES-256 SDCTR server->client encryption
    . 2019-10-23 18:58:24.855 Initialised HMAC-SHA-256 server->client MAC algorithm
    ! 2019-10-23 18:58:25.361 Using username "(My/Sftp username)".
    . 2019-10-23 18:58:25.594 Server offered these authentication methods: publickey,password,keyboard-interactive
    . 2019-10-23 18:58:25.594 Attempting keyboard-interactive authentication
    . 2019-10-23 18:58:25.825 Server refused keyboard-interactive authentication
    . 2019-10-23 18:58:25.825 Server offered these authentication methods: publickey,password,keyboard-interactive
    . 2019-10-23 18:58:25.826 Prompt (password, "SSH password", <no instructions>, "&Password: ")
    . 2019-10-23 18:58:25.826 Using stored password.
    . 2019-10-23 18:58:25.827 Sent password
    . 2019-10-23 18:58:26.064 Access granted
    . 2019-10-23 18:58:26.064 Opening session as main channel
    . 2019-10-23 18:58:26.577 Opened main channel
    . 2019-10-23 18:58:27.082 Started a shell/command
    . 2019-10-23 18:58:27.082
    . 2019-10-23 18:58:27.082 Using SFTP protocol.
    . 2019-10-23 18:58:27.082 Doing startup conversation with host.
    > 2019-10-23 18:58:27.082 Type: SSH_FXP_INIT, Size: 5, Number: -1
    < 2019-10-23 18:58:27.316 Type: SSH_FXP_VERSION, Size: 150, Number: -1
    . 2019-10-23 18:58:27.316 SFTP version 3 negotiated.
    . 2019-10-23 18:58:27.317 Unknown server extension posix-rename@openssh.com="1"
    . 2019-10-23 18:58:27.317 Supports statvfs@openssh.com extension version "2"
    . 2019-10-23 18:58:27.317 Unknown server extension fstatvfs@openssh.com="2"
    . 2019-10-23 18:58:27.317 Supports hardlink@openssh.com extension version "1"
    . 2019-10-23 18:58:27.317 Unknown server extension fsync@openssh.com="1"
    . 2019-10-23 18:58:27.317 We believe the server has signed timestamps bug
    . 2019-10-23 18:58:27.317 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
    . 2019-10-23 18:58:27.317 Limiting packet size to OpenSSH sftp-server limit of 262148 bytes
    . 2019-10-23 18:58:27.317 Getting current directory name.
    . 2019-10-23 18:58:27.318 Getting real path for '.'
    > 2019-10-23 18:58:27.318 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
    < 2019-10-23 18:58:27.550 Type: SSH_FXP_NAME, Size: 23, Number: 16
    . 2019-10-23 18:58:27.550 Real path is '/'
    . 2019-10-23 18:58:27.550 Startup conversation with host finished.
    < 2019-10-23 18:58:27.550 Script: Active session: [1] (My/Sftp username)@(My host name)
    > 2019-10-23 18:58:27.745 Script: pwd
    < 2019-10-23 18:58:27.745 Script: /
    > 2019-10-23 18:58:27.811 Script: put -nopermissions -preservetime -transfer="binary" -filemask="*.Csv" -- "C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP" "/out"
    . 2019-10-23 18:58:27.813 Copying 1 files/directories to remote directory "/"
    . 2019-10-23 18:58:27.813 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: out
    . 2019-10-23 18:58:27.813 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: *.Csv; ResumeL: 0
    . 2019-10-23 18:58:27.813 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
    . 2019-10-23 18:58:27.813 Getting real path for '/'
    > 2019-10-23 18:58:27.813 Type: SSH_FXP_REALPATH, Size: 10, Number: 272
    < 2019-10-23 18:58:28.045 Type: SSH_FXP_NAME, Size: 23, Number: 272
    . 2019-10-23 18:58:28.045 Real path is '/'
    . 2019-10-23 18:58:28.045 File: 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP' [2019-10-23T10:03:38.249Z] [0]
    . 2019-10-23 18:58:28.046 Listing file "/out".
    > 2019-10-23 18:58:28.046 Type: SSH_FXP_LSTAT, Size: 13, Number: 519
    < 2019-10-23 18:58:28.277 Type: SSH_FXP_ATTRS, Size: 37, Number: 519
    . 2019-10-23 18:58:28.277 out;D;0;2019-10-23T07:36:16.000Z;3;"" [1005];"" [1004];rwxrwxr-x;0
    . 2019-10-23 18:58:28.278 File: 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP\TestCSVDocument.csv' [2019-10-16T07:28:41.000Z] [52]
    . 2019-10-23 18:58:28.278 Copying "C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP\TestCSVDocument.csv" to remote directory started.
    . 2019-10-23 18:58:28.278 Binary transfer mode selected.
    . 2019-10-23 18:58:28.278 Opening remote file.
    > 2019-10-23 18:58:28.279 Type: SSH_FXP_OPEN, Size: 49, Number: 771
    < 2019-10-23 18:58:28.510 Type: SSH_FXP_HANDLE, Size: 13, Number: 771
    > 2019-10-23 18:58:28.510 Type: SSH_FXP_WRITE, Size: 77, Number: 1286
    > 2019-10-23 18:58:28.511 Type: SSH_FXP_CLOSE, Size: 13, Number: 1540
    > 2019-10-23 18:58:28.511 Type: SSH_FXP_SETSTAT, Size: 45, Number: 1033
    < 2019-10-23 18:58:28.742 Type: SSH_FXP_STATUS, Size: 24, Number: 1286
    < 2019-10-23 18:58:28.742 Status code: 0
    < 2019-10-23 18:58:28.974 Type: SSH_FXP_STATUS, Size: 24, Number: 1540
    < 2019-10-23 18:58:28.974 Status code: 0
    . 2019-10-23 18:58:28.974 Preserving timestamp [2019-10-16T07:28:41.000Z]
    < 2019-10-23 18:58:28.974 Type: SSH_FXP_STATUS, Size: 24, Number: 1033
    < 2019-10-23 18:58:28.974 Status code: 0
    . 2019-10-23 18:58:28.974 Transfer done: 'C:\Users\maniratnamkumarm\Desktop\3PL DEMO\ForSFTP\TestCSVDocument.csv' => '/out/TestCSVDocument.csv' [52]
    . 2019-10-23 18:58:28.974 Copying finished: Transferred: 52, Elapsed: 0:00:01, CPS: 0/s
    > 2019-10-23 18:58:29.486 Script: exit
    . 2019-10-23 18:58:29.486 Script: Exit code: 0
    . 2019-10-23 18:58:29.487 Closing connection.
    . 2019-10-23 18:58:29.487 Sending special code: 12
    . 2019-10-23 18:58:29.487 Sent EOF message

    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav

    According to the logfile the upload was ok:

    3ghfdpiyxvmn.png

    Regards.
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    I am very very happy with your help...
    Thankssss
    Its coming.DOne.....
    [If you wish,plse share your personal mail id for immediate help :) this is my mail id: maniratnamkr1@gmail.com]

    Thanks,
    Mani.
  • ftorneroftornero Member Posts: 522
    Hello @ManiNav,

    I will send you my personal email but I think that is better that you continue posting here your doubts so other people can benefit for the answers that you receive.

    Regards
  • ManiNavManiNav Member Posts: 120
    Hi ftornero,

    Thanks, Sure; I will post continue here my doubts as of your suggestion.

    Thanks,
    Mani.
  • NunoSilvaNunoSilva Member Posts: 15
    Hi,
    Is it possible to run it on the server? I already install the WinSCP on the server and copy the DLL to addins folder, but I still get the same error when I didn't have installed yet on the server.
    thanks in advance
    NGS
  • ftorneroftornero Member Posts: 522
    Hello @NunoSilva,

    Could you elaborate a little what it's the error that you are getting ?

    Regards.
  • NunoSilvaNunoSilva Member Posts: 15
    Hi,
    The error is this one

    Cannot create an instance of the following .NET Framework object: assembly WinSCPnet, Version=1.7.2.10414, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf, type WinSCP.SessionOptions.

    and it is raised on the first line of code.

    WinSCPSessionOptions := WinSCPSessionOptions.SessionOptions;
    WinSCPSessionOptions.HostName := SessionHostName;
    WinSCPSessionOptions.UserName := SessionUser;
    WinSCPSessionOptions.Password := SessionPassword;
    WinSCPSessionOptions.Protocol := WinSCPProtocol.Ftp;
    WinSCPSessionOptions.FtpMode := WinSCPFTPMode.Active;

    All the variables are declared RunOnClient = NO

    If I run the same code on stage server I don't get the error, I thought it was because we didn't start the service. Yesterday I could make a restart of the service but I still get the same error.

    @RockWithNAV I saw someone on the comments of the link you post to register on the server, to do that I only need to run the setup program or I need to do something else?
  • ftorneroftornero Member Posts: 522
    Hello @NunoSilva ,

    Do you have the WinSCPnet DLL in the Server Add-in folder ?

    Regards
  • NunoSilvaNunoSilva Member Posts: 15
    Yes in C:\Program Files\Microsoft Dynamics NAV\100\Service\Add-ins\WinSCPnet\
Sign In or Register to comment.