web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Create flat file on the fly and reuse it for storing

(0) ShareShare
ReportReport
Posted on by 160

I need via a batch process to create a flat file from AX and store it via document management (Cloud or sharepoint).

To generate a file a physical location is required or on the cloud we can not a priori store file, before storing it via document management.

Examples that exist always pass through an external file upload not from a file ont the fly.

Do you have ideas for generating a file on the fly and reusing it for storage?

*This post is locked for comments

I have the same question (0)
  • geminikartik Profile Picture
    241 on at
    RE: Create flat file on the fly and reuse it for storing

    Hi ,

    Create a table like Document history where you can store all your outgoing files and you can check the content of file as well . Once you able to generate file from code , use that file name and attached to Docuref  . Check below and let me know if it helps.

    str filepath           = DMFParameters::find().SharedFolderPath;

    Filename _fileNameSave  = strFmt("%1"filepath , '.txt');

    public static void LogFile_Delegate(filename            fileNameSave  ,

                                                                                           EventHandlerResult  _res)

       {

           DocuRef     docuref;

           BinData     binData = new BinData();

           str         path;

           str         nameOfFile;

           str         extention;

           EventLogTable  logfile;

           EventLogTable  updatelogfile;

           int         Sleepcounter;

           if(fileNameSave  != null)

           {

               [path,nameOfFile,extention] = fileNameSplit(fileNameSave  );

               logfile.clear();

               logfile.FileName = nameOfFile;

               logfile.LineNum = EventLogTable::lastLineNum()+1;

               logfile.FileID = WinAPIServer::createGUID();

               logfile.insert();

               binData.loadFile(fileNameSave  );

               container cc = binData.getData();

               System.IO.Stream  stream = Binary::constructFromContainer(cc).getMemoryStream();

               docuref = DocumentManagement::attachFileToCommon(logfile, "File", stream, fileNameSave  , 'txt',fileNameSave  );

               ttsbegin;

               logfile.selectForUpdate(true);

               logfile.Refrecid = docuref.RecId;

               logfile.doUpdate();

               ttscommit;

                   }

    }

  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    RE: Create flat file on the fly and reuse it for storing

    But how did you check the binary data you put there? Did you use a hex editor, or how?

    FileIOPermission won't make any difference, as we're not working with a file.

  • Foxsor Profile Picture
    160 on at
    RE: Create flat file on the fly and reuse it for storing

    @Martin: I check the file when i dowload it from the document management with the web client interface from document management from my custable record

    @Sukrut: The add of permission didn't change anithing same result

    I try with a:

    InteropPermission perm = new InteropPermission( InteropKind::ClrInterop);

    >Didn't work

  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    RE: Create flat file on the fly and reuse it for storing

    How do you "check" the file? Maybe you're doing it wrong.

  • Foxsor Profile Picture
    160 on at
    RE: Create flat file on the fly and reuse it for storing

    I agree with you i explore the data stream way,so i test a piece of code for use a data stream with a cloud location (data management).

    The attachement is succeed (docuref and cloud url created) but when i check the file result from the web client with open file , there is no data in it :(

           System.IO.Stream     stream;

           TextStreamIo            textStreamIo;

           container                  cLine;

           DocuRef                   addedRecord;

           CustTable                 custTable;

           FileName                 fileName;

           str                            fileContentType;

           fileName = "TESTDocu.csv";

           fileContentType = "text/csv";

           custTable = custTable::find("TEST");

           if(custTable)

           {

               try

               {

                   ttsbegin;

                   cLine = conNull();

                   cLine = conIns(cLine,1,"MyDataLine1"); // One line of data

                   textStreamIo = TextStreamIo::constructForWrite(1252);

                   textStreamIo.outFieldDelimiter('');

                   textStreamIo.outRecordDelimiter('\r\n');

                   textStreamIo.write(cLine);  // Add data to stream

                   //textStreamIo.writeRaw("Test2");

                   textStreamIo.finalize();

                   stream = textStreamIo.getStream();

                   // DocumentManagement add

                   addedRecord = DocumentManagement::attachFile( custTable.TableId,custTable.RecId,custTable.DataAreaId, 'FlatFile', stream, fileName, fileContentType,"Csv file attached");

                   if(!addedRecord)

                   {

                       throw error("Failed to attach");

                   }

                   ttscommit;

               }

               catch (Exception::UpdateConflict)

               {

                   throw Exception::UpdateConflict;

               }

               catch (Exception::DuplicateKeyException)

               {

                   throw Exception::DuplicateKeyException;

               }

               catch (Exception::Deadlock)

               {

                   throw Exception::Deadlock;

               }

               catch

               {

                   throw error("Treatment failed");

               }

               info("End process");

           }

       }

  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    RE: Create flat file on the fly and reuse it for storing

    I disagree. There is no reason for saving data to disk just to be able to store them in a blob storage. And indeed, DocumentManagement.attachFile() requires only a name (which is for identification in the storage; it's not a file path to any existing file) and the content is provided in a data stream (so there is no reason for having an file which you would open and read data from there in another stream; the data is provided in this parameter). Do you have a different experience?

    If you say that *you* create a file and stored it on disk, it's your thing. It's not a requirement for adding data to document management.

  • Foxsor Profile Picture
    160 on at
    RE: Create flat file on the fly and reuse it for storing

    Thank's Martin.

    "Document management doesn't require a file; you can use a data stream instead."

    Yes but when you create on the fly a file inside AX,  you need to record it with a specific location, before use with a data stream,isn't it?

  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    RE: Create flat file on the fly and reuse it for storing

    Document management doesn't require a file; you can use a data stream instead.

    If you want to create a temporary file before uploading it to storage, it should be possible as usual (although I haven't really tried it in AX 7).

    Also, you surely can use Azure storage to store data; it's exactly what document management does. It utilizes blob storage; there is also file storage.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Community Member Profile Picture

Community Member 2

#2
Mea_ Profile Picture

Mea_ 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans