Friday, December 30, 2016

AX 2012 AX 2009 and Secure FTP / SFTP Integration AX2012 AX2009

This post demonstrates how to integrate AX 2009/2012 with Secure FTP.

I have used the WINSCP tool to achieve SFTP file read and write

Steps overview:-
    1. Download WINSCP and Place it in proper folder (For instance folder name is WINSCP)
    2. Give AOS service account full permission to the WINSCP folder (this is required when you run it in Batch)
    3. Run/Configure WINSCP.exe, - This is a mandatory setup
    4. Copy the host key - this key is used in X++ to call the SFTP
    5. Create a bat and script file for execution inside WINSCP folder - You can handle this in X++ code
    6. You can use the same folder to download/upload the file

Below code used to get the file from SFTP

static void SFTP_GETFILES(Args _args)
{
    TextIo                  textIoFile;
    Set                     permissionSet;
    FileName                filePath, batchFileName, scriptFileName;
    InteropPermission       interopPerm;

    System.Diagnostics.Process              process;
    System.Diagnostics.ProcessStartInfo     processStartInfo;
    System.Exception ex;
    ;
    filePath = 'C:\\WINSCP\\'; //WINSCP folder

    batchFileName = filePath + 'readfile.bat';
    scriptFileName = filePath + 'readfileScript.txt';

    try
    {
 
        //Creating BAT File
        permissionSet = new Set(Types::Class);
        permissionSet.add(new FileIoPermission(batchFileName, "W"));
        CodeAccessPermission::assertMultiple(permissionSet);
        textIoFile = new TextIo(batchFileName , "W", 0);
        CodeAccessPermission::revertAssert();
        textIoFile.write(strFmt('winscp.com /script=%1',scriptFileName));
        textIoFile = null;
     

        //Creating SCRIPT File
        permissionSet = new Set(Types::Class);
        permissionSet.add(new FileIoPermission(scriptFileName, "W"));
        CodeAccessPermission::assertMultiple(permissionSet);
        textIoFile = new TextIo(scriptFileName , "W", 0);
        CodeAccessPermission::revertAssert();
        textIofile.write('open sftp://sftp_username:#sftp_password@sftp.xyz.com -hostkey="ssh-rsa 1024 cd:Host key"');
        textIoFile.write(strFmt('get /SFTP_FOLDERNAME/* %1', filePath));     //get the file from SFTP
        //textIoFile.write(strFmt('mv /SFTP_FOLDERNAME/*.txt /SFTP_FOLDERNAME/Archive/')); //Use this code to move the file inside SFTP
//textIoFile.write(strFmt('put %1 /SFTP_FOLDERNAME/', filePath + "\\" + "Uploadfile.txt"));
//Use this code to upload the file to SFTP
        textIoFile.write('exit');
        textIoFile = null;
     

        //EXECUTE SFTP
        new InteropPermission(InteropKind::ClrInterop).assert();
        process = new System.Diagnostics.Process();
        processStartInfo = new System.Diagnostics.ProcessStartInfo();
        processStartInfo.set_FileName(batchFileName);
        processStartInfo.set_WorkingDirectory(filePath);
        process.set_StartInfo(processStartInfo);
        process.Start();//this method call the SFTP
        process.WaitForExit();
        CodeAccessPermission::revertAssert();

        interopPerm = new InteropPermission(InteropKind::ClrInterop);
        interopPerm.assert();
        System.IO.File::Delete(batchFileName);
        System.IO.File::Delete(scriptFileName);
        CodeAccessPermission::revertAssert();

    }
    catch (Exception::CLRError)
    {
         ex = ClrInterop::getLastException();
         if (ex != null)
         {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }
    }
}

You can refer WINSCP portal for more details and commands
Hope this helped you to understand the AX and SFTP integration.

Happy DAXing,

Cheers,
Arun

6 comments:

  1. Dear Arun,

    I am having the requirement like below.(ie)

    A batch job will create four text files in a folder. I have to copy and paste that files to another location. for this now i am doing through WINSCP manually. If i want to automate this process using SFTP means what i need to do?.

    In your post i'm not getting the 5th point.
    5. Create bat and script file for execution inside WINSCP folder - You can handle this in X++ code

    Could you please shed some light on this

    Thanks & Regards,
    Gopalakrishnan

    ReplyDelete
  2. Above job will create 2 files 1. Bat file 2. Script file. Script will have SFTP command and file details. Bat file calls WINSCP and execute instruction inside the Script file.
    If you run the above job it will produce those 2 files that will give a clear understanding. Refer WINSCP site for the commands you required.

    ReplyDelete
  3. HI Arun,
    Could you please provide more information about the second point AOS credentials?
    Kindly let me know how we can provide AOS credentials to WINSCP folder

    ReplyDelete
    Replies
    1. I actually forgot the step. Give AOS service account full permission in the folder and try.

      Delete
    2. Could you please let me know on which folder I have to give full permission

      Delete
    3. The folder where you place WINSCP bat and script files to execute your commands and load file. Refer WINSCP website for more information about setup.

      Delete