I need to created a comma delimited text file with variables from a form, and store it in a specific location. Trying to figure out the best way to go about doing this. Any suggestions?
Question
Question
Is it possible to generate a .txt file with the variables captured in a form?
Answer
Hi David,
First off, if you're not familiar with C# or VB.NET, I would go with C# since I usually have an easier time finding C# examples and it is more familiar to anyone with experience using other C based languages like Java or JavaScript.
The actual code is going to depend on how you actually need to build the file. For example, is all the data going in one line, or will you have line breaks?
Once you switch the scripting language to C# .NET, the first thing you want to do is add a reference to System.IO
To reduce the amount of work you need to do in building the script code, I would use a Token to build the actual content of the text file.
For example,
1. Create a token called "FilePath" and use that to build the destination path of the file (including the file name) like you have in your example.
When you use tokens in a token with "\" characters, you have to double up on the backslash to escape the special characters in the token format. For example, something like \\SERVER\FOLDER\\%(Token1)\\%(Token2).txt
EDIT: On second thought, I don't think you need to escape in string tokens, just in certain activities like HTTP Web Request URLs. Test it out first to make sure.
2. In the same Token activity create a new multivalue token called something like "TextValues" and add each form value to a new line in the multivalue token. If you need quotes around the token values, just wrap them in quotes
3. Create another new token called "TextContent" and add the "TextValues" token, right-click the token, open the Token Editor, and select "Apply Index > All values separated by Comma"
4. Finally, in your script activity you can reference the FilePath token for the path and the TextContent token when you write to the file.
protected override void Execute() { // Write your code here. string path = GetTokenValue("FilePath").ToString(); string text = GetTokenValue("TextContent").ToString(); File.WriteAllText(path,text); }
EDIT: Added the missing ".ToString()" at the end of the GetTokenValue methods.
Again, there's a lot to watch out for because your specific needs might require some things to be changed and you'll need to be careful about permissions because running the workflow will use the workflow user account, but testing the script in the designer will use your credentials.
The big advantage of building everything in tokens outside of the script is that you can use Track Token activities to check the values and make sure you get the expected results before you try the script and every time it runs.
If you did everything inside the script, you'd have to add more code just to be able to see what values it was actually getting for the path and text content.
If you do a search for "C# write a text file" you can find a ton of examples that could help you make any adjustments you might need.
Replies
Maybe with a Script Activity in Workflow.
- Create a workflow with
- Retrieve Business Process Variables activity to get your form field values
- A Script activity with reference to System.IO, then add C# or VB.NET code to write the values to a text file in your preferred format and save it to the desired location
- Make sure your Workflow services account has permission to write to the destination
- Trigger the workflow at the end of your form process
Jason,
I think your answer is the solution I need, but I'm really not familiar with VB.Net. Do you think you could provide some sample code to help get me started down the right path? When I insert the 'Script' activity, it defaults to this:
Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Data.SqlClient Imports System.Text Namespace WorkflowActivity.Scripting.Script '''<summary> '''Provides one or more methods that can be run when the workflow scripting activity is performed. '''</summary> Public Class Script1 Inherits ScriptClass90 '''<summary> '''This method is run when the activity is performed. '''</summary> Protected Overrides Sub Execute() 'Write your code here. End Sub End Class End Namespace
The location I'm trying to save to is:
\\INTRANET\labels\inspection\'variable from Retrieve Business Process'\'variable2 from Retrieve Business Process'.txt
I need it to create a text file that has quotes around each variable from the 'Retrieve Business Process Variables' activity and is comma delimited, then stores it in that location. So... create a comma delimited text file with variables and store it somewhere. Any help is greatly appreciated.
Hi David,
First off, if you're not familiar with C# or VB.NET, I would go with C# since I usually have an easier time finding C# examples and it is more familiar to anyone with experience using other C based languages like Java or JavaScript.
The actual code is going to depend on how you actually need to build the file. For example, is all the data going in one line, or will you have line breaks?
Once you switch the scripting language to C# .NET, the first thing you want to do is add a reference to System.IO
To reduce the amount of work you need to do in building the script code, I would use a Token to build the actual content of the text file.
For example,
1. Create a token called "FilePath" and use that to build the destination path of the file (including the file name) like you have in your example.
When you use tokens in a token with "\" characters, you have to double up on the backslash to escape the special characters in the token format. For example, something like \\SERVER\FOLDER\\%(Token1)\\%(Token2).txt
EDIT: On second thought, I don't think you need to escape in string tokens, just in certain activities like HTTP Web Request URLs. Test it out first to make sure.
2. In the same Token activity create a new multivalue token called something like "TextValues" and add each form value to a new line in the multivalue token. If you need quotes around the token values, just wrap them in quotes
3. Create another new token called "TextContent" and add the "TextValues" token, right-click the token, open the Token Editor, and select "Apply Index > All values separated by Comma"
4. Finally, in your script activity you can reference the FilePath token for the path and the TextContent token when you write to the file.
protected override void Execute() { // Write your code here. string path = GetTokenValue("FilePath").ToString(); string text = GetTokenValue("TextContent").ToString(); File.WriteAllText(path,text); }
EDIT: Added the missing ".ToString()" at the end of the GetTokenValue methods.
Again, there's a lot to watch out for because your specific needs might require some things to be changed and you'll need to be careful about permissions because running the workflow will use the workflow user account, but testing the script in the designer will use your credentials.
The big advantage of building everything in tokens outside of the script is that you can use Track Token activities to check the values and make sure you get the expected results before you try the script and every time it runs.
If you did everything inside the script, you'd have to add more code just to be able to see what values it was actually getting for the path and text content.
If you do a search for "C# write a text file" you can find a ton of examples that could help you make any adjustments you might need.
Jason,
You're amazing! Seriously, thank you so much for the help. This did exactly what I needed. The only change I had to make was adding ".ToString()" to the end of each:
string path = GetTokenValue("FilePath"); string text = GetTokenValue("TextContent");
so it became:
string path = GetTokenValue("FilePath").ToString(); string text = GetTokenValue("TextContent").ToString();
It's creating and inputting the data exactly as expected. I really appreciate the thorough answer.
I new I'd forget something haha. I always do!
I do something similar to what Jason is talking about with Workflow. It's very handy to know how to do. Alternatively, you could use the Fill PDF activity in Workflow. That would eliminate the scripting:
Hope this helps.
Hi Jason
I am doing something very similar to David and have it working successfully using the same method you recommended. I am retrieving table values from a form I have created and populating a txt file which will be used as a report, the issue I have run into is when the user adds a 2nd table row. It works perfectly with one table row but as soon as a 2nd row is added the txt file will contain data structured like this:
|Inv0001 | Inv0002 | R100 | R200|
Instead of like this:
|Inv0001 | R100 |
|Inv0002 | R200 |
Do you have any recommendations, I have reason to believe that I can fix it with the token formatting, but I could be wrong.
It's hard to say exactly without seeing the code and workflow activities.
The example I provided flattens the token values into a single string, so if you want line breaks you need to format them into that string.
The original poster was looking for a simple list of comma-separated values so formatting the values together with the comma was enough.
However, in your case there would seem to be two columns and multiple rows, so you can't use the same approach.
Instead, you'll need to loop through each row of your table data and build each "row" for the multivalue token similar to Step 2.
For each row, append the values as a single line to the multivalue token so you can build out your rows with the corresponding values together.
|%(ForEachRow_Column1)|%(ForEachRow_Column2)|
Then you can use the token formatting to join those values on a Line Break instead of a comma like in Step 3.