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

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Verified answer
    Rachit Profile Picture
    4,015 User Group Leader on at

    Hi Mahesh,

    You have many options to choose from, one approach I would suggest here is:

    1. To loop through files use System.IO.Directory from X++. Refer to this nice blog http://www.artofcreation.be/2009/04/08/winapi-rpc-1702-and-findfirstfilew/

    2. For creating attachments use document handling. Just google it and you can find many blogs having code how to do it. One nice blog is http://microsoftdax.blogspot.com.au/2012/06/x-code-for-document-attachement-in-ax.html

    Hope this helps!!

  • Verified answer
    Community Member Profile Picture
    on at

    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,...).

  • Community Member Profile Picture
    on at

    Okay Rachit Garg and jens I will try it.

  • Suggested answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    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

    I will check it Faisal.

  • Verified answer
    syed baber Profile Picture
    11,633 on at

    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

    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?

  • Community Member Profile Picture
    on at

    Thanks to all for your help.

  • Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    Excellent - it worked for you.

  • Community Member Profile Picture
    on at

    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.  

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans