- Overview
- Custom activities
- Migrating Activities to .NET 6
- Release Notes
- Building Workflow Analyzer Rules
- Building Activities Project Settings
- Creating Custom Wizards
- Prioritize Activities by Scope
- UiPath.Activities.Api.Base
- UiPath.Studio.Activities.Api
- UiPath.Studio.Activities.Api.Activities
- UiPath.Studio.Activities.Api.BusyService
- UiPath.Studio.Activities.Api.ExpressionEditor
- UiPath.Studio.Activities.Api.Expressions
- UiPath.Studio.Activities.Api.Licensing
- UiPath.Studio.Activities.Api.Mocking
- UiPath.Studio.Activities.Api.ObjectLibrary
- UiPath.Studio.Activities.Api.PackageBindings
- UiPath.Studio.Activities.Api.ProjectProperties
- UiPath.Studio.Activities.Api.ScopedActivities
- UiPath.Studio.Activities.Api.Settings
- UiPath.Studio.Activities.Api.Wizards
- UiPath.Studio.Activities.Api.Workflow
- UiPath.Studio.Api.Controls
- UiPath.Studio.Api.Telemetry
- UiPath.Studio.Api.Theme
- Robot JavaScript SDK
- Triggers SDK
- Agents SDKs

Developer Guide
UiPath.Examples.Activities solution on GitHub. The activity:
-
Takes two numbers and an operation (add, subtract, multiply, or divide) as input
-
Returns the result of the selected operation
The activity code consists of two parts:
UiPath.Activities.Template, and rename the solution plus all related files and references to UiPath.Examples.Activities.- Rename the file that holds the activity logic from
ActivityTemplate.cstoCalculator.cs. -
Update the references and namespace as follows:
using System.Activities; using System.Diagnostics; using UiPath.Examples.Activities.Helpers; namespace UiPath.Examples.Activities { }using System.Activities; using System.Diagnostics; using UiPath.Examples.Activities.Helpers; namespace UiPath.Examples.Activities { } -
Declare the input arguments - two numbers (
FirstNumberandSecondNumberasint) and the operation to perform (SelectedOperationasenum, with an optional default value set toMultiply). Mark all three arguments as required using the[RequiredArgument]attribute. The returned value is used to set the value of theResultargument:public class Calculator : CodeActivity<int> // This base class exposes an OutArgument named Result { [RequiredArgument] public InArgument<int> FirstNumber { get; set; } //InArgument allows a varriable to be set from the workflow [RequiredArgument] public InArgument<int> SecondNumber { get; set; } [RequiredArgument] public Operation SelectedOperation { get; set; } = Operation.Multiply; // default value is optional /* * The returned value will be used to set the value of the Result argument */ }public class Calculator : CodeActivity<int> // This base class exposes an OutArgument named Result { [RequiredArgument] public InArgument<int> FirstNumber { get; set; } //InArgument allows a varriable to be set from the workflow [RequiredArgument] public InArgument<int> SecondNumber { get; set; } [RequiredArgument] public Operation SelectedOperation { get; set; } = Operation.Multiply; // default value is optional /* * The returned value will be used to set the value of the Result argument */ } -
Start the execution part, add logging, get the values of the numbers from the workflow context, and add logic to handle the scenario of a division by zero:
protected override int Execute(CodeActivityContext context) { // This is how you can log messages from your activity. logs are sent to the Robot which will forward them to Orchestrator context.GetExecutorRuntime().LogMessage(new Robot.Activities.Api.LogMessage() { EventType = TraceEventType.Information, Message = "Executing Calculator activity" }); var firstNumber = FirstNumber.Get(context); //get the value from the workflow context (remember, this can be a variable) var secondNumber = SecondNumber.Get(context); if (secondNumber == 0 && SelectedOperation == Operation.Divide) { throw new DivideByZeroException("Second number should not be zero when the selected operation is divide"); } return ExecuteInternal(firstNumber, secondNumber); }protected override int Execute(CodeActivityContext context) { // This is how you can log messages from your activity. logs are sent to the Robot which will forward them to Orchestrator context.GetExecutorRuntime().LogMessage(new Robot.Activities.Api.LogMessage() { EventType = TraceEventType.Information, Message = "Executing Calculator activity" }); var firstNumber = FirstNumber.Get(context); //get the value from the workflow context (remember, this can be a variable) var secondNumber = SecondNumber.Get(context); if (secondNumber == 0 && SelectedOperation == Operation.Divide) { throw new DivideByZeroException("Second number should not be zero when the selected operation is divide"); } return ExecuteInternal(firstNumber, secondNumber); } -
Add the calculations to execute for each selected operation:
public int ExecuteInternal(int firstNumber, int secondNumber) { return SelectedOperation switch { Operation.Add => firstNumber + secondNumber, Operation.Subtract => firstNumber - secondNumber, Operation.Multiply => firstNumber * secondNumber, Operation.Divide => firstNumber / secondNumber, _ => throw new NotSupportedException("Operation not supported"), }; }public int ExecuteInternal(int firstNumber, int secondNumber) { return SelectedOperation switch { Operation.Add => firstNumber + secondNumber, Operation.Subtract => firstNumber - secondNumber, Operation.Multiply => firstNumber * secondNumber, Operation.Divide => firstNumber / secondNumber, _ => throw new NotSupportedException("Operation not supported"), }; } -
Define the operations:
public enum Operation { Add, Subtract, Multiply, Divide }public enum Operation { Add, Subtract, Multiply, Divide }
The available inputs in an activity are determined by the data types of the properties. In the sample Calculator activity:
-
The
intdata type of theFirstNumberandSecondNumberproperties results in number editor input fields for these properties. -
For the
Operationproperty, which has theenumdata type, a dropdown menu is available in the activity.
These data types directly influence how users interact with the inputs of the activity.
Resources.resx file.
The following table describes the most common properties available for each activity property:
| Property | Description |
|---|---|
| DisplayName | The label of the property. |
| Tooltip | The text to display when hovering over the property |
| IsRequired1 | Whether the property is required. Required properties must also be marked in the activity using the [RequiredArgument] attribute.
|
| IsPrincipal2 | Whether the property should always be visible in the main category of the activity. If set to false, the property appears under a Show advanced options menu that is collapsed by default.
|
| OrderIndex | The order in which to display the property. |
1 Not available for output properties, which are never mandatory.
2 By convention, output properties are placed at the end of the activity under advanced options.
Creating the design for the Calculator activity
- Rename the file
ActivityTemplateViewModel.cstoCalculatorViewModel.cs, and add the code for the activity user interface to it. -
Update the references and namespace as follows:
using System.Activities.DesignViewModels; using System.Diagnostics; namespace UiPath.Examples.Activities.ViewModels { }using System.Activities.DesignViewModels; using System.Diagnostics; namespace UiPath.Examples.Activities.ViewModels { } -
Declare the input properties. The result property comes from the base class of the activity. The names and type arguments must match the ones from the activity:
public class CalculatorViewModel : DesignPropertiesViewModel { /* * Properties names must match the names and generic type arguments of the properties in the activity * Use DesignInArgument for properties that accept a variable */ public DesignInArgument<int> FirstNumber { get; set; } public DesignInArgument<int> SecondNumber { get; set; } /* * Use DesignProperty for properties that accept a constant value */ public DesignProperty<Operation> SelectedOperation { get; set; } /* * The result property comes from the activity's base class */ public DesignOutArgument<int> Result { get; set; } public CalculatorViewModel(IDesignServices services) : base(services) { } }public class CalculatorViewModel : DesignPropertiesViewModel { /* * Properties names must match the names and generic type arguments of the properties in the activity * Use DesignInArgument for properties that accept a variable */ public DesignInArgument<int> FirstNumber { get; set; } public DesignInArgument<int> SecondNumber { get; set; } /* * Use DesignProperty for properties that accept a constant value */ public DesignProperty<Operation> SelectedOperation { get; set; } /* * The result property comes from the activity's base class */ public DesignOutArgument<int> Result { get; set; } public CalculatorViewModel(IDesignServices services) : base(services) { } } -
Add the code for the activity design. Optionally, to debug
ViewModelinitialization, add a breakpoint by uncommenting the line containingDebugger.Break();.The following code snippet initializes the properties of theViewModel, adds aPersistValuesChangedDuringInit()call which is mandatory when you change the property values during initialization, and defines the activity input and output properties:protected override void InitializeModel() { //Debugger.Break(); /* * The base call will initialize the properties of the view model with the values from the xaml or with the default values from the activity */ base.InitializeModel(); PersistValuesChangedDuringInit(); // just for heads-up here; it's a mandatory call only when you change the values of properties during initialization var orderIndex = 0; FirstNumber.DisplayName = Resources.Calculator_FirstNumber_DisplayName; FirstNumber.Tooltip = Resources.Calculator_FirstNumber_Tooltip; /* * Required fields will automatically raise validation errors when empty. * Unless you do custom validation, required activity properties should be marked as such both in the view model and in the activity: * -> in the view model use the IsRequired property * -> in the activity use the [RequiredArgument] attribute. */ FirstNumber.IsRequired = true; FirstNumber.IsPrincipal = true; // specifies if it belongs to the main category (which cannot be collapsed) FirstNumber.OrderIndex = orderIndex++; // indicates the order in which the fields appear in the designer (i.e. the line number); SecondNumber.DisplayName = Resources.Calculator_SecondNumber_DisplayName; SecondNumber.Tooltip = Resources.Calculator_SecondNumber_Tooltip; SecondNumber.IsRequired = true; SecondNumber.IsPrincipal = true; SecondNumber.OrderIndex = orderIndex++; SelectedOperation.DisplayName = Resources.Calculator_SelectedOperation_DisplayName; SelectedOperation.Tooltip = Resources.Calculator_SelectedOperation_Tooltip; SelectedOperation.IsRequired = true; SelectedOperation.IsPrincipal = true; SelectedOperation.OrderIndex = orderIndex++; /* * Output properties are never mandatory. * By convention, they are not principal and they are placed at the end of the activity. */ Result.DisplayName = Resources.Calculator_Result_DisplayName; Result.Tooltip = Resources.Calculator_Result_Tooltip; Result.OrderIndex = orderIndex; }protected override void InitializeModel() { //Debugger.Break(); /* * The base call will initialize the properties of the view model with the values from the xaml or with the default values from the activity */ base.InitializeModel(); PersistValuesChangedDuringInit(); // just for heads-up here; it's a mandatory call only when you change the values of properties during initialization var orderIndex = 0; FirstNumber.DisplayName = Resources.Calculator_FirstNumber_DisplayName; FirstNumber.Tooltip = Resources.Calculator_FirstNumber_Tooltip; /* * Required fields will automatically raise validation errors when empty. * Unless you do custom validation, required activity properties should be marked as such both in the view model and in the activity: * -> in the view model use the IsRequired property * -> in the activity use the [RequiredArgument] attribute. */ FirstNumber.IsRequired = true; FirstNumber.IsPrincipal = true; // specifies if it belongs to the main category (which cannot be collapsed) FirstNumber.OrderIndex = orderIndex++; // indicates the order in which the fields appear in the designer (i.e. the line number); SecondNumber.DisplayName = Resources.Calculator_SecondNumber_DisplayName; SecondNumber.Tooltip = Resources.Calculator_SecondNumber_Tooltip; SecondNumber.IsRequired = true; SecondNumber.IsPrincipal = true; SecondNumber.OrderIndex = orderIndex++; SelectedOperation.DisplayName = Resources.Calculator_SelectedOperation_DisplayName; SelectedOperation.Tooltip = Resources.Calculator_SelectedOperation_Tooltip; SelectedOperation.IsRequired = true; SelectedOperation.IsPrincipal = true; SelectedOperation.OrderIndex = orderIndex++; /* * Output properties are never mandatory. * By convention, they are not principal and they are placed at the end of the activity. */ Result.DisplayName = Resources.Calculator_Result_DisplayName; Result.Tooltip = Resources.Calculator_Result_Tooltip; Result.OrderIndex = orderIndex; } - Add the string values for the labels and tooltips in the
Resources.resxfile. For localization purposes, you must use specific comments for the activity name (Activity name) and activity description (Activity description). For other strings, adding comments is recommended.
The following image shows the activity configuration as displayed in UiPath Studio:
1 - Labels (display names) of the three input properties.
FirstNumber property.