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 use C# Code building blocks as shown below:
Clicking this field opens up a popup which 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 he can make use of LogMessage() multiple times and then run the code.
There are Three Tabs in C# Code editor first is the Source Code Tab(as explained above) and 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 Source Code, and at the same time, user can use fields to pass data as output from 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 this external reference or dependencies. For example, suppose a user is looking to parse a JSON file using C# code block, for that user need to add 'Newtonsoft.Json.dll' to parse the JSON string. To add that the user 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 created on 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.