C#
The C# action An instruction within a botflow. runs C# code in a botflow Automated steps that you can design for each bot that will run.. You must be familiar with Microsoft's C# programming language to supply the C# code necessary to add a C# action to a botflow.
The C# action is useful for creating an action not supplied with Nintex RPA LE.
Explore the items listed below to learn more about the basics of adding a C# action to a botflow, and how to use the different methods and functions available in the C# action:
- Add a C# action to a botflow: Lists the basic steps needed to add a C# action to a botflow.
- Code method example: Provides an example for using the C# action Code method to add C# code directly to a C# action.
- File method example: Provides an example for using the C# action File method to add C# code saved in a file to a C# action.
- RPAEngine.SetVar example: Provides an example for using the RPAEngine.SetVar function to set Nintex RPA LE variables A spot to store a piece of information..
- RPAEngine.GetVar example: Provides an example for using the RPAEngine.GetVar function to get Nintex RPA LE variables.
- RPAEngine.LoadDLL example: Provides an example for using the RPAEngine.LoadDLL function to load a dll at runtime.
Add a C# action to a botflow
To add a C# action to a botflow:
- Start Nintex RPA LE. You can do this from the Start Menu under Nintex or from the Nintex RPA LE shortcut on the Desktop.
- On the Actions list (located under the Selection Tool ), click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Set the C# Action Options/Settings.
- Method: Select a Method from the drop-down list:
- Code: Select Code from the drop-down list to display the Code field.
- File: Select File from the drop-down list to display the File field.
- Timeout: Set the number of seconds in the Timeout field to stop the C# code should it enter an infinite loop and fail to end.
- Code: The Code field displays when you select Code as the Method. Type your C# code into the Code field to compile and run your C# code as part of the C# action. Or use the Expression Builder () to build the action settings using variables or other token values. The C# code is compiled as a temporary .exe file, executed, and then deleted when the botflow run completes.
- File: The File field displays when you select File as the Method. Type the file location into the File field, use the Folder () to select the file location, or use the Expression Builder () to build the action settings using variables or other token values. The File method works better when you have a large amount of code to execute.
- Save to: It is recommended that you save the C# action results to a Variable. Select the Save to check box to activate the Save to field. Type the variable name in the Save to field, select the variable from the drop-down list, or use the Expression Builder () to build the action settings using variables or other token values. Nintex RPA LE supports C# statement outputs of "Console.WriteLine" or "Console.Write".
- If needed, adjust the Run Switch to add the action without running it.
- Optionally, add an action Note.
- Click OK to add the C# action to the botflow.
This C# code example prints a message to a Nintex RPA LE Variable using the Nintex RPA LE C# action Code method.
- If not already running, start Nintex RPA LE.
- Create a variable called Output.
- Click Add Item () on the Botflow Pane.
- Click Variable. The Variable window displays.
- Type Output in the Name field.
- Click OK to save the variable.
- On the Actions list, click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Select Code from the Method drop-down list.
- Click the Copy link below, and copy and paste the C# code from the window that appears into the Code field.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World");
}
}
}
- Select the Save to check box to activate the Save to field and select the Output variable from the Save to drop-down list.
- Set the Run Switch to Don't Run.
- Click OK to add the C# action to the botflow.
- Click Play () at the bottom of the Nintex RPA LE window to run the botflow.
The words "Hello World" display in the Output variable Value field.
This C# code example prints a message to a Nintex RPA LE Variable using the Nintex RPA LE C# action File method.
- If not already running, start Nintex RPA LE.
- Create a variable called Output.
- Click Add Item () on the Botflow Pane.
- Click Variable. The Variable window displays.
- Type Output in the Name field.
- Click OK to save the variable.
- Click the Copy link below, and copy and paste the C# code from the window that appears into Notepad. Save the Notepad file to your Desktop with the name csharpexample.txt. Normally, a programmer would use Visual Studio or a similar application to write C# code, but we will use Notepad for this example.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World");
}
}
}
- On the Actions list, click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Select File from the Method drop-down list.
- Click the Folder () in the File field, navigate to your Desktop, and select the Notepad file called csharpexample.txt you created in step 3. Select Open to save the cshartexample.txt file location to the File field.
- Select the Save to check box to activate the Save to field and select the Output variable from the Save to drop-down list.
- Set the Run Switch to Don't Run.
- Click OK to add the C# action to the botflow.
- Click Play () at the bottom of the Nintex RPA LE window to run the botflow.
The words "Hello World" display in the Output variable Value field.
The C# action comes with a feature used to set a variable's Value field.
Use the function "RPAEngine.SetVar" with the syntax : RPAEngine.SetVar("RPA_Variable_Name", CS_Value)
To create the C# action using the RPAEngine.SetVar function:
- If not already running, start Nintex RPA LE.
- Create a variable called Output.
- Click Add Item () on the Botflow Pane.
- Click Variable. The Variable window displays.
- Type Output in the Name field.
- Click OK to save the variable.
- Create three variables called Variable1, Variable2, and Variable3.
- Click Add Item () on the Botflow Pane.
- Click Variable. The Variable window displays.
- Type Variable1 in the Name field.
- Select Number in the Type field drop-down list.
- Click OK to save the variable.
- Repeat steps a through e to create variables named Variable2 and Variable3.
- On the Actions list, click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Select Code from the Method drop-down list.
- Click the Copy link below, and copy and paste the C# code from the window that appears into the Code field.
using System;
namespace Program
{
class Calculation
{
static void Main()
{
Int32 x = 1, y = 2;
RPAEngine.SetVar("Variable1", x);
RPAEngine.SetVar("Variable2", y);
RPAEngine.SetVar("Variable3", x + y);
Console.WriteLine("SUCCESS");
}
}
}
- Select the Save to check box to activate the Save to field and select the Output variable from the Save to drop-down list.
- Set the Run Switch to Don't Run.
- Click OK to add the C# action to the botflow.
- Click Play () at the bottom of the Nintex RPA LE window to run the botflow.
The number "1" displays in the Variable1 variable Value field. The number "2" displays in the Variable2 variable Value field. The number "3" displays in the Variable3 variable Value field. The word "SUCCESS" displays in the Output variable Value field.
The C# action comes with a feature used to:
- Get a variable's Value field value.
- Set another variable's Value field with that value.
Use the function "RPAEngine.GetVar" with the syntax : RPAEngine.GetVar("RPA_Variable_Name", CS_Value)
To create the C# action using the RPAEngine.GetVar function:
- If not already running, start Nintex RPA LE.
- Create two variables called Variable1 and Variable2.
- Click Add Item () on the Botflow Pane.
- Click Variable. The Variable window displays.
- Type Variable1 in the Name field.
- Select Number in the Type field drop-down list.
- Type "1111" in the Value field.
- Click OK to save the variable.
- Repeat steps a through f to create a variable named Variable2, leaving the Value field blank.
- On the Actions list, click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Select Code from the Method drop-down list.
- Click the Copy link below, and copy and paste the C# code from the window that appears into the Code field.
using System;
namespace Program
{
class Test
{
static void Main()
{
String strValue = "0";
RPAEngine.GetVar("Variable1", strValue);
RPAEngine.SetVar("Variable2", strValue);
}
}
}
- Set the Run Switch to Don't Run.
- Click OK to add the C# action to the botflow.
- Click Play () at the bottom of the Nintex RPA LE window to run the botflow.
The number "1111" is copied from the Variable1 variable Value field and pasted into the Variable2 variable Value field.
The C# action comes with a feature used to load a dll.
Use the function "RPAEngine.LoadDLL" with the syntax : RPAEngine.LoadDLL("Path")
To create the C# action using the RPAEngine.LoadDLL function:
- If not already running, start Nintex RPA LE.
- On the Actions list, click Advanced and then click C# from the Programming list. The Action Builder window for C# displays.
- Select Code from the Method drop-down list.
- Click the Copy link below, and copy and paste the C# code from the window that appears into the Code field.
RPAEngine.LoadDLL("System.Windows.Forms.dll")
using System.Windows.Forms;
namespace HelloWorld
{
class Hello
{
static void Main()
{
MessageBox.Show("Hello World");
}
}
}
- Set the Run Switch to Don't Run.
- Click OK to add the C# action to the botflow.
- Click Play () at the bottom of the Nintex RPA LE window to run the botflow.
A message window displays "Hello World."
Field or button | Description |
---|---|
C# Action Options/Settings |
Set the C# action options and settings:
|
Note |
(Optional) Type a Note to document any special instructions or functionality.
|
Run Switch |
The Run Switch toggle controls how Nintex Bot runs an action when adding or editing an action in a botflow.
|
OK/Cancel | Click OK to save the action or click Cancel to discard the action or any changes. |