Send Files over SFTP With a Code Activity

This tutorial keeps the older code-based SFTP approach available for users who want to handle SFTP directly in a code activity. If you prefer a packaged activity, use the SFTP Activities Extension Library instead.

Using It In A Workflow

1. Download the WinSCP support files from WinSCPnet.zip and place WinSCPnet.dll where your Integration Soup code activity can reference it.
2. Open your workflow and add a Run Code activity after the step that produces the content you want to upload.
3. Right click in the activity message template and choose Insert Activity Message so the outgoing content flows in from the previous step.
4. Paste the code below into the code activity and replace the server details, host fingerprint, and authentication settings with your own values.
5. Save the workflow and test it with a message so you can confirm the file arrives at the remote destination.

Code Activity Example

This example uploads the current activity message as a file using WinSCP. It uses the workflow variable CurrentDateTime to create the file name.

#r "C:\Program Files (x86)\Popokey\Integration Host Server\Custom Libraries\WinSCPnet.dll"

System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream);
string message = activityInstance.Message.Text;
writer.Write(message);
writer.Flush();
stream.Position = 0;

string filetype = ".hl7";
string filename = workflowInstance.GetVariable("CurrentDateTime");

WinSCP.SessionOptions sessionOptions = new WinSCP.SessionOptions
{
    Protocol = WinSCP.Protocol.Sftp,
    HostName = "myftpserver.com",
    UserName = "MyUser",
    SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxxxxxxxxxxxxxxxxxx",
    SshPrivateKeyPath = @"path\to\my\key.ppk"
};

using (WinSCP.Session session = new WinSCP.Session())
{
    session.Open(sessionOptions);
    session.PutFile(stream, filename + filetype);
}

What To Change In The Example

Update HostName, UserName, SshHostKeyFingerprint, and SshPrivateKeyPath to match your SFTP server. If your server uses password authentication instead of a private key, replace the private key path with a password setting in the session options.

You can also replace the file naming logic with your own pattern, for example by using another workflow variable or a fixed file name.

Helpful Notes

This code-focused approach is useful when you want full control of the SFTP logic inside a code activity. For most users, the SFTP Extension Library is simpler because it exposes the connection settings as normal activity parameters.

SFTP Activities Extension Library

Return to the Tutorials Directory