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.
- FTP Setup – to configure the connection details.
- FTP Setup Line – to configure the methods like upload, download and file paths.
- FTP Log
FTP Setups:
FTP Setup:
You can create multiple FTP Setup for different connections.
- Active field can be used to Activate / Deactivate the FTP Setup.
- Sequence field can be used to setup the sequence in which the FTP batch job can process the FTP Setup.
- Connection can be tested using Test Connection.
FTP Setup Line:
- Active field can be used to Activate / Deactivate individual FTP Setup Line.
- Line level Sequence field can be used to setup the sequence in which Setup Line can be processed.
- Uploaded files can be archived & deleted using the “Archive & Delete” option.
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 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 .
Batch job can be scheduled in Job Queue:
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
Is it possible to download the fobs please?
LikeLike
Hi Darko,
Sorry that i cannot share this. 😦 But Let me know if you need some help.
LikeLike
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.
LikeLike
Hi Vanish,
Sorry that I cannot share the object. But let me know if you have any queries. Thanks
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
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
LikeLike
Hello Divesh Bora,
i need the FTP Management Codeunit.
Can you please send me this via Email.
I need the Fob Objects please.
Thank you so much.
SchwarzMailde@gmx.de my Mail Adress.
Best Regards
René
LikeLike
Hello Divesh Bora,
will you be so kind and send me the fob files? Great Work btw!
BR
LikeLike