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 :
Dynamics 365 Community / Blogs / Dynamics 365 Tech Blog / Create and save csv or text...

Create and save csv or text file in D365 Finance and Operations

feelyd Profile Picture feelyd 260

In this example, we are going to create a CSV or Text File in Dynamics 365 Finance and Operations. We're going to fill the file with customer records from the CustTable table and download the file locally using your browser. Lets start by adding a new runnable class to our Visual Studio Project. Right Click project name and select add.Step 1 image

Lets add a new class to our Visual Studio Project. Right Click project name. Right Click our project in Visual Studio, if you don't know how to create a project refer back to our earlier tutorials 

Step 2 image

In the pop up window or menu items which is displayed , Click the Add menu item.Step 3 image

Click New Item... In the pop up window which is displayed I am going to select code in the left hand window. This will show me all the code options I can add.Step 4 image

Select runnable class in the code pane options and give the class a name. I am going to call the class RunnableClassCreateCSV Step 5 image

Click Name at the bottom of the dialog or window to give the class the name you would like.Step 6 image

In the Name insert the name of the class RunnableClassCreateCSV Step 7 image

In the code pane of the class I'll paste some code i prepared earlierStep 8 image

The code we will insert in in the screen shot below. The full code is at the bottom of the article as well  We start by creating a text stream object. the next line then writes to the object and populates it with the header of our file using the write method.We then open the CustTable making sure we are in the USRT company , this is an optional steps and just swithces the company to the one sepecified. We then initialise a while look and populate all the customer records into the stream method using the same write method we used previously. 

Now we have a stream of content we need to read it before it can be send to a file. The stream reader does this. When the stream reader is initialised it is with the text stream object. Reading the  Content to the end converts this to our XML object.

The sendstringasfiletouser method of the file object does the magic and sends the file to the browser.Step 9 image

Comments

*This post is locked for comments