SFTP for NAV 2009

Hi,

I was using FTP on classic client which was working fine
Now it has been changed to SFTP
can you please tell me how can I use SFTP
WinSCP does not work on classic client 2009
Any other standard/free api/automation ?

Thanks
Ajay Jain
UK

Answers

  • txerifftxeriff Member Posts: 492
    edited 2018-11-29
    Well, I´ve done sftp implementation for some customer in the past I recall I used winscp dotnet variable and adapted a piece of code, I hope this helps. Sftp key can be found when logging from windows client, you can store it in customer card or like this. Dotnet variables can be found in winscp website, they need to go to service folder so they distribute automatically

    About classic client, the only way I see is to call a exposed function from NAV WS so this way it will run RTC.
    :


    PROCEDURE SFTP@1000000026(Operation@1000000000 : 'Upload,List,Delete';pLogin@1000000010 : Text[50];pPassword@1000000009 : Text[50];pLocalFile@1000000008 : Text[250];pRemoteFile@1000000007 : Text[250];pLogFile@1000000004 : Text[250];pFTPDirectory@1000000011 : Text[250]) vSuccess : Boolean;
    VAR
    WinSCPSessionOptions@1000000006 : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.SessionOptions";
    WinSCPSesh@1000000005 : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.Session";
    WinSCPTransferOptions@1000000003 : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOptions";
    WinSCPTransferResults@1000000001 : DotNet "'WinSCPnet, Version=1.3.7.7601, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf'.WinSCP.TransferOperationResult";
    vHost@1000000002 : Text;
    vPort@1000000012 : Integer;
    vRemoteFile@1000000013 : Text;
    BEGIN

    vSuccess:=FALSE;
    gsFTPError:=FALSE;
    WinSCPSessionOptions := WinSCPSessionOptions.SessionOptions;
    vHost:=COPYSTR(gHost,7,STRLEN(gHost)); //remove port. we start after ftp://
    EVALUATE(vPort,COPYSTR(vHost,STRPOS(vHost,':')+1,STRLEN(vHost))); //copy port
    vHost:=COPYSTR(vHost,1,STRPOS(vHost,':')-1); //copy only until port
    WinSCPSessionOptions.HostName(vHost);
    WinSCPSessionOptions.PortNumber(vPort);
    WinSCPSessionOptions.UserName(gLogin);
    WinSCPSessionOptions.Password(gPassword);

    //amend remote file
    //full string to amend: 'ftp://XXX.XXX.XX.XX:22/uploads/Inventory.csv'
    vRemoteFile:=COPYSTR(pRemoteFile,STRPOS(pRemoteFile,':')+1,STRLEN(pRemoteFile)); //copy till first :
    vRemoteFile:=COPYSTR(vRemoteFile,STRPOS(vRemoteFile,':')+3,STRLEN(vRemoteFile));

    //run winscp windows client and you will find the following key there
    WinSCPSessionOptions.SshHostKeyFingerprint(sFTPKey);

    WinSCPSesh:=WinSCPSesh.Session;
    WinSCPSesh.Open(WinSCPSessionOptions);


    WinSCPTransferOptions := WinSCPTransferOptions.TransferOptions();
    WinSCPTransferOptions.TransferMode:=0;
    CASE Operation OF
    Operation::Upload: BEGIN
    //premotefile needs to be cut the string
    WinSCPTransferResults:=WinSCPSesh.PutFiles(pLocalFile,vRemoteFile,FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults:=WinSCPSesh.GetFiles(pRemoteFile,pLocalFile,FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults.Check();
    vSuccess:=WinSCPTransferResults.IsSuccess();
    IF vSuccess=FALSE THEN BEGIN
    WriteToErrorLog(pLogFile,FORMAT(WinSCPTransferResults.ToString(),1024));
    gsFTPError:=TRUE;
    END;
    END;
    Operation::List:
    BEGIN
    vSuccess:=WinSCPSesh.FileExists(vRemoteFile);
    // IF vSuccess THEN
    // WinSCPSesh.RemoveFiles(pRemoteFile);
    END;
    Operation::Delete: BEGIN
    IF WinSCPSesh.FileExists(vRemoteFile) THEN BEGIN
    WinSCPSesh.RemoveFiles(vRemoteFile);
    WinSCPTransferResults:=WinSCPSesh.GetFiles(vRemoteFile,TEMPORARYPATH+'tmpfile.tmp',FALSE,WinSCPTransferOptions);
    // WinSCPTransferResults.Check();
    vSuccess:=WinSCPTransferResults.IsSuccess();
    IF vSuccess=FALSE THEN
    vSuccess:=TRUE; //if it cant find means the file is deleted properly
    END
    ELSE
    vSuccess:=TRUE;
    END;
    END;

    END;

    PROCEDURE SetSFTP@1100270000(psFTP@1100270000 : Boolean;pSftpKey@1100270001 : Text);
    BEGIN
    gsFTP:=psFTP;
    sFTPKey:=pSftpKey;

    END;



    More details:

    https://community.dynamics.com/nav/b/researchdesigndevelop/archive/2017/10/31/file-upload-sftp

    https://winscp.net/eng/docs/library#vbnet
  • ajayjainajayjain Member Posts: 119
    Thanks very much treff
    I have done in classic client, it works in the same way as used to be
    you can write the dos comments in batch/txt file and execute

    Thanks
    Ajay
    Ajay Jain
    UK
  • abrar273abrar273 Member Posts: 6
    Hi Ajay,

    Can you please share some details about it.
    SFTP through batch file.

    Thanks in advance
  • fgffgf Member Posts: 2
    Hi Please Help Me
    I added the dll ( WinSCP for SFTP) to windows. I created a new automation. But the methods of this automaition are not listed. Don't see metod list.
Sign In or Register to comment.