C# Code

The C# Code block lets you run custom C# logic that is not directly available through standard LEAPWORK building blocks, for example parsing a JSON object or reading values from XML. This block is useful when a scenario is easier to solve through code than by combining standard blocks.

Fully expanded, the C# Code block shows the following properties:

image-20251222-124346.png

Note: The screenshot on this page uses the Elegance Design, introduced in 2025.3. If you are using an earlier version, your layout may look different.

Quick-start

  1. Drag C# Code onto the canvas.

  2. Connect the block in the flow and optionally specify the basic parameters required for this block to operate, listed by their exact UI labels in one line: Click to edit, Text fields, Default timeout, Timeout (sec). For details on how to work with the editor modal, see

  3. Run the flow when it’s ready.

Building block parameters

Parameter
  • Block header: Shows the current name of the C# Code block. You can rename it by double-clicking the header and typing a new title.

  • Click to edit: Opens a popup editor for C# code. A sample code snippet is available to show the entry point for code to be written. Write your code in the editor and select Run to compile and execute it. If you want to debug the code, you can call LogMessage() multiple times and then run the code to inspect intermediate results.

  • Text fields: Stores key value pairs that can contain dynamic content and be used as tokens. These fields are maintained as Dictionary<string, object>. You can collect and type cast values in your C# code as required. The fields you add here are also shown in the editor under the Fields tab.

  • Error occurred: Triggers if a runtime error occurs while executing the C# code during case execution.

  • Default timeout: Controls how the block timeout is determined. If Default Timeout is unchecked, the timeout value is 60 seconds. If Default Timeout is checked, the flow setting for Default timeout is applied.

  • Timeout (sec): Sets the maximum time allowed for executing the code, including processing input and producing output. If execution is unsuccessful within the specified time, the block triggers Error occurred.

    Note: All cases have a global timeout that can be configured in the Settings panel. This is unrelated to the timeout of a single building block. A running case is automatically cancelled if it runs longer than the global timeout.

Using the editor tabs

Source code

Use this tab to write the C# logic. Select Run to compile and execute the code. If you need to debug, you can add multiple LogMessage() calls and run the code to inspect intermediate values.

image-20251222-124724.png


Fields

Fields are used to provide input and output data from code. You create them in the block’s Text fields parameter and they appear in this tab for review and editing. In code, they are available through a dictionary structure and can be collected and type cast as needed.

image-20251222-130942.png


References

Before writing code against an external component or connected service, the block must include a reference to it. Use this tab to add external references or dependencies. For example, parsing JSON may require adding Newtonsoft.Json.dll.

Download the DLL and use From file to add it. You can also add files from the GAC when available.

image-20251222-131023.png

Examples

Use JSON_XML_Parser.zip as a reference package. The zip contains two sample flows that demonstrate how to parse JSON and XML using the C# Code block.

Import the package and reuse or adapt the flows for your own scenarios.

JSON_XML_Parser.zip


Resources

Topic

Description

Flows FAQ

Common questions about creating, running, and managing flows in Leapwork.

Flows Troubleshooting

Guidelines and solutions for identifying and fixing issues that occur when building or running flows in Leapwork.