Tech Tuesday: Creating a Task in SmartConnect to get a file from an FTP site
This weeks Tech Tuesday is from our very own Chris Dew, our Director of Product Management, who will explain how to create a task in SmartConnect to get a file from an FTP site.
We get quite a few customers that want to take files from a FTP site and utilize this file as the source of their data when running a map inside of SmartConnect. Today I have created a sample Task script that will allow you to grab a file in a Pre Map Task and then use it for the integration.
First you can simply click on the Tasks button in any existing map, and then choose a new task called run script. This is where you can now copy the script below and paste into this window (like in the image below).
‘EXAMPLE OF HOW TO USE FtpWebRequest
Const localFile As String = “C:\Users\Administrator\Downloads\ftp\README.txt”
Const remoteFile As String = “/README.txt”
Const host As String = “ftp://ftp.swfwmd.state.fl.us/pub/”
Const username As String = “anonymous”
Const password As String = “firstname.lastname@example.org”
‘Create a request
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
‘Set the credentials
ftp.Credentials = New System.Net.NetworkCredential(username, password)
‘Turn off KeepAlive (will close connection on completion)
ftp.KeepAlive = False
‘we want a binary
ftp.UseBinary = True
‘Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
‘If we were using a method that uploads data e.g. UploadFile
‘we would open the ftp.GetRequestStream here an send the data
‘Get the response to the Ftp request and the associated stream
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
‘loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 ‘see Note(1)
The only configuration that needs to be done is at the very top where you determine four things:
1) The name and location you want to put the file on your network
2) The name of the remote file
3) FTP Site
Have fun with this one and use your imagination on other scripts you can use. If you have a suggestion for a new script please let me know.
Interested in SmartConnect? Please email us at email@example.com and we would be happy to assist you!
Leave a Comment
Integrate & Automate without Any Code.
SmartList Data has Never Been Faster.
The Easiest Way to Report on GP Data.
When can we start seeing the SmartView and SmartList Builder in a web Client in or outside of Dynamics GP?