We will use a separate workflow to read the configuration parameters and pass them to the main workflow. So, let's create the new sequence file by clicking on New on the UiPath Studio ribbon and choosing the Sequence option. Name the new sequence ReadConfig.xaml and click on Create.
In this sequence file, we will open the Config.xlsx file that we created in the previous section, read the values, and store them as Dictionary arguments. Then, we will pass this Dictionary variable with the populated key-value pairs back to Main for the automation process to use.
Let's open the ReadConfig.xaml sequence and step through how to create the workflow:
- Add an input String argument called in_ConfigFile with a default value of Config.xlsx. Add an Output argument of the Dictionary type called out_Config.
- Add the following variables. Ensure that the direction and type of argument match:
- We will start the workflow by adding the Try-Catch block for exception handling. In the Try block, let's initialize the Dictionary argument before we use it.
- Let's use the Assign activity and set out_Config to new Dictionary(of String, object).
- Next, let's use the Read Range activity under the workbook to read the config.xlsx file's content. Your workflow should look as follows:
- Set the properties of the Read Range activity as follows:
- Leave the Range property blank as we want to read all the content in the sheet.
- Add the SheetName – we named it "Constants".
- In the WorkbookPath, add the in_ConfigFile input argument.
- Check AddHeaders.
- For Output | DataTable, create a new variable called dtConfigTable (use Ctrl + K).
- The Read Range activity helped us read the value of an Excel range and store it in the DataTable named dtConfigTable. Now, let's iterate the dtConfigTable and store it in the output Dictionary argument.
- We will use the For Each Row activity to read each row from the DataTable and update the key-value pairs in the dtConfigTable Dictionary argument.
- As we read each row, we'll omit the empty rows and only get values that are not null. To do that, we need to add an If control activity and add a condition stating NOT string.IsNullOrEmpty(row("Name").ToString.Trim). Your workflow should now look like this:
- If the rows are not null, then you'll need to populate the Dictionary variable. We'll use the Assign activity to do so – let's name it Add key/value pair. In this activity, we will assign out_Config(row("Name").ToString.Trim) to row("Value"). This will take the row values from the DataTable and store them in the output Dictionary argument.
- Finally, let's go to the Catch block and handle the system exception. For that, choose System.exception and add a log message at the Trace level stating "No Constants defined for the process".
Now that we have a workflow that can be used to read the configuration file, let's invoke this sequence from Main and test whether the values are retrieved.