Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

(0) ShareShare
ReportReport
Posted on by

Hello I have a task.

I want to run a X++ job that much identify a folder say in C drive. The folders will be numbered like personnel numbers in HcmWoker table. Each folder may consists of some files of types PDF,word,jpeg and so on.

The X++ job must go to the particular folder and add all these files as attachments to the particular record numbered according to the folder name(personnel number).

Can anybody give some ideas? How I can begin? Or C# is suitable for this? Or you have X++ code?

For the movement I am able to attach documents when I run the X++ job, I will browse manually and find the file and attach it to the document.

Now I wanted this process must get automated that whenever I run the Job the job must automatically go and upload all the files.

*This post is locked for comments

  • Faisal Fareed Profile Picture
    10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    No trouble - Am glad your requirement get resolved. CHeers

  • Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Yes Fareed. I was not able to see your solution. But I will try to use later the execution time  calculation which is used in the link you have given.  

  • Faisal Fareed Profile Picture
    10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Excellent - it worked for you.

  • Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Thanks to all for your help.

  • Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Baber and all,

    By following different things I am able to attach documents.

    Now the context is that I want to attach certificates for the employees in HcmWorker table may contain documents like scanned certificates(jpeg,mpeg) PDF files and so on.

    The documents will be arranged in a folder named according to the  personnel number field in HCMWorker table like 000001,000542 and so on. In each of these folders documents to be uploaded for each employee will be there.

    Next thing. What maximum file sizes combinedly can I attach? All other ideas as per your experiences?

  • Verified answer
    syed baber Profile Picture
    11,633 on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Mahesh,

    Recenlty, I did the same thing in one of the project I was working on. Below is the code to traverse through the directory and return the files in container variable:

    private container getListOfImportFiles()

    {

       System.Array                files;

       InteropPermission           permission;

       counter     fileCounter;

       container   fileList;

       ;

       permission  = new InteropPermission(InteropKind::ClrInterop);

       permission.assert();

       files = CLRInterop::staticInvoke("System.IO.Directory", "GetFiles", YourDirectoryPath, "*.csv");

       for (fileCounter = 0; fileCounter < ClrInterop::getAnyTypeForObject(files.get_Length()); fileCounter++)

       {

           fileList = conins(fileList, conLen(fileList)+1, ClrInterop::getAnyTypeForObject(files.GetValue(fileCounter)));

       }

       CodeAccessPermission::revertAssert();

       return fileList;

    }

    After this, you can write the code to read the files one by one. Code is shown below:

    FileName            importFileName;

    Counter     counter;

       container   fileList;

       FileIOPermission    permission;

       CommaTextIo         file;

       container           line;

       int                 recordCount;

    if (conlen(fileList) > 0)

       {

           for (counter =1; counter <= conlen(fileList); counter++)

           {

               importFileName = conPeek(fileList, counter);

               permission = new FileIOPermission(importFileName, "RW" );

               permission.assert();

               file = new CommaTextIo(importFileName, #io_read);

               if (!file || file.status() != IO_Status::Ok)

               {

                   throw error("File not found");

               }

               file.inFieldDelimiter(',');

               line = file.read();

               while (file.status() == IO_Status::Ok)

               {

                   recordCount++;

                   // process the file here by using the container variable 'line'

                   line = file.read();

               }

               file = null;          

               CodeAccessPermission::revertAssert();

           } // End for loop

       } // End outer If clause

    For document attachment, refer to the below code:

    private void attachDoc(RefTableId _refTableId, RefRecId _refRecId, selectableDataArea _refCompanyId, FileName _name)

    {  

       DocuRef docuRef;

       DocuActionArchive archive;

       FileName fileName;

       ;

       // Get the file name only.

       fileName = conPeek(fileNameSplit(_name), 2);

       docuRef.clear();

       docuRef.RefRecId = _refRecId;

       docuRef.RefTableId = _refTableId;

       docuRef.RefCompanyId = _refCompanyId;

       docuRef.Name = fileName;

       docuRef.TypeId = YourTypeID;

       docuRef.insert();

       archive = new DocuActionArchive();

       archive.insertDocuValue(docuRef, _name);

       //archive.add(docuRef, _name);

    }

    Please verify and let me know if you have further queries.

    Thanks,

    Baber.

  • Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    I will check it Faisal.

  • Suggested answer
    Faisal Fareed Profile Picture
    10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Mahesh,

    I did this bit of work few days ago and blog it here daxture.blogspot.com.au/.../retrieving-files-from-directory-through.html

    For attaching it as attachments please have a look on DocuRef class as suggested by Rachit in above link.

    If you need more help please do let me know

  • Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Okay Rachit Garg and jens I will try it.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Check the WINAPI class, it contains functions to operate Folders.

    There are also .NET functions you can use e.g. check out the System.IO.Directory Namespace/class.

    To attach the document to a record you have to create the Docu* table records (DocuRef, DocuValue,...).

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,120 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans