The C# Code building block can be used to perform custom logical operations that are not directly available with the regular LEAPWORK building blocks, such as parsing a JSON object, reading values from XML, etc. The C# Code building block comes in handy for users who want to handle their specific scenarios by relying on programming.
To write the C# code in LEAPWORK, the user needs to utilize the C# Code building block, as shown below:
Clicking this field opens up a popup that is an editor for C# code. A sample code snippet is also available, which shows the entry point for code to be written. Write your code in this editor and press the “Run” button to compile and execute it. Also, if a user wants to debug the code, then they can make use of LogMessage() multiple times, then run the code.
There are Three Tabs in the C# Code editor. The first is the Source Code Tab (as shown above), the other two are Fields and References. Let's go through them one by one:
Fields are used to provide input and output data from code. Users can create the input fields to feed data to the Source Code, and at the same time the user can use fields to pass data as the output from the source code. Users can also add any number of fields that can contain dynamic content and be used as tokens. This is maintained in the form of Dictionary <string, object>. These fields can be collected and typecast for their usage in C# code as required.
Before the user writes code against an external component or connected service, the LEAPWORK Code Block must first contain a reference to it. The reference section takes care of adding the external reference or dependencies. For example, if a user is looking to parse a JSON file using the C# code block, they need to add 'Newtonsoft.Json.dll' to parse the JSON string. The user also has to download the 'Newtonsoft.Json.dll' and Click on 'From file' to add the .dll. Users can also add the files from GAC, as shown below.
We have created two sample flows which include custom logic, to show how to parse the JSON file and XML file using the C# Code building block. You can refer to these examples and create your custom logic using C# code.
If you have any questions, contact our Priority Support.