- 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
Creating Activities With Code (Legacy)
There are two major steps required to create a custom activity:
- Writing the custom activity code.
- Adding the external assembly (.dll) in UiPath.
The following components are required to create a custom activity:
- Microsoft Visual Studio with the .NET desktop development workload installed.
- NuGet Package Explorer.
To better understand how to write the code for a custom activity, we are going to create a simple activity which asks the user for two numbers, then outputs the square of their sum.
- Launch Microsoft Visual Studio.
- Click File > New >Project… (shortcut: Ctrl + Shift + N). The New Project window is displayed.
- Select C# from the Languages dropdown menu. The list of all dependencies using C# is displayed.
- Select Class Library (.NET Framework). This helps us export the custom activity as a
.dll
file. Click Next to go to the Configure your new project window. - Fill in the Project name field with the desired activity name. In our case, we can use “MathSquareOfSum”.
- Select .NET Framework 4.6.1 from the Framework dropdown menu. This ensures that the library is compatible with UiPath Studio.
- Click Create to go to the code designer and start writing the activity code.
- In the Solution Explorer panel, right-click the References branch and select Add Reference.... The Reference Manager window is displayed.
-
Search for the
System.Activities
andSystem.ComponentModel.Composition
references and add them. Make sure you check the box in front of each of them and click OK. Doing so enables us to use classes from the aforementioned references. -
Now, we need to make sure that our code uses the newly-added references. This is done by adding the following lines in the code designer:
using System.Activities; using System.ComponentModel;
using System.Activities; using System.ComponentModel;Our project should now look like this:
-
Add the input and output parameters. In our case, the code should look like this:
//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English //Dots allow for hierarchy. App Integration.Excel is where Excel activities are. [Category("Category.Where.Your.Activity.Appears.In.Toolbox")] [DisplayName("Human readable name instead of class name")] [Description("The text of the tooltip")] public class MathSqSum : CodeActivity { //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English [Category("Input")] [DisplayName("First Number")] [Description("Enter the first number")] [RequiredArgument] public InArgument<int> FirstNumber { get; set; } [Category("Input")] [DisplayName("Second Number")] [Description("Enter the second number")] [RequiredArgument] public InArgument<int> SecondNumber { get; set; } [Category("Output")] public OutArgument<int> ResultNumber { get; set; } protected override void Execute(CodeActivityContext context) { }
//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English //Dots allow for hierarchy. App Integration.Excel is where Excel activities are. [Category("Category.Where.Your.Activity.Appears.In.Toolbox")] [DisplayName("Human readable name instead of class name")] [Description("The text of the tooltip")] public class MathSqSum : CodeActivity { //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English [Category("Input")] [DisplayName("First Number")] [Description("Enter the first number")] [RequiredArgument] public InArgument<int> FirstNumber { get; set; } [Category("Input")] [DisplayName("Second Number")] [Description("Enter the second number")] [RequiredArgument] public InArgument<int> SecondNumber { get; set; } [Category("Output")] public OutArgument<int> ResultNumber { get; set; } protected override void Execute(CodeActivityContext context) { }
<DisplayName(" ")>
attribute is the label shown before the input field in the Properties panel in Studio. The <Description(" ")>
attribute is the text of the tooltip displayed on mouse hover.
<ReguiredArgument>
attribute is needed if you want a declared element to be mandatory for input. If it is not filled in, a blue caution icon
appears in the title bar of the activity.
<Execute( )>
overridden function. In our case, it looks like this:
protected override void Execute(CodeActivityContext context)
{
var firstNumber = FirstNumber.Get(context);
var secondNumber = SecondNumber.Get(context);
var result = (int)Math.Pow(firstNumber + secondNumber, 2);
ResultNumber.Set(context, result);
}
protected override void Execute(CodeActivityContext context)
{
var firstNumber = FirstNumber.Get(context);
var secondNumber = SecondNumber.Get(context);
var result = (int)Math.Pow(firstNumber + secondNumber, 2);
ResultNumber.Set(context, result);
}
If you do not wish for your activity to have a designer interface, you can skip to the building the library section.
In order to build a Designer Interface, you need to have the Windows Workflow Foundation component installed in Visual Studio. If you have not selected the component from the Visual Studio installer, you can add it as follows:
- In Visual Studio, click the Tools menu, and select Get Tools and Features.... The Visual Studio Installer window shows up.
- Switch to the Individual Components tab and search for the Windows Workflow Foundation component. It is located under the Development activities section.
- Check the box in front of the Windows Workflow Foundation component and click Modify. The required component gets installed.
13. Right-click the project from the Properties panel (in our case, the project is MathSquareOfSum). The context menu shows up.
14. From the Add item, select New Item.... The Add New Item window is displayed.
15. Click on Workflow under the Installed category on the left panel. All related elements are displayed.
16. Select Activity Designer and click Add to include the item in the project.
.xaml
file is opened right away. It should look like this:
17. Replace the existing Activity Designer code with the following:
<sap:ActivityDesigner x:Class="MathSquareOfSum.MathSqSumDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
<a href="sap:ActivityDesigner.Resources">sap:ActivityDesigner.Resources</a>
<ResourceDictionary>
<sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter" />
</ResourceDictionary>
</sap:ActivityDesigner.Resources>
<DockPanel Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"></ColumnDefinition>
<ColumnDefinition Width="210"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="First Number"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter first number" Expression="{Binding Path=ModelItem.FirstNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Second Number"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter second number" Expression="{Binding Path=ModelItem.SecondNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Result"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="The sum of the numbers" UseLocationExpression="True" Expression="{Binding Path=ModelItem.ResultNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=Out, Mode=TwoWay}" />
</Grid>
</DockPanel>
</sap:ActivityDesigner>
<sap:ActivityDesigner x:Class="MathSquareOfSum.MathSqSumDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
<a href="sap:ActivityDesigner.Resources">sap:ActivityDesigner.Resources</a>
<ResourceDictionary>
<sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter" />
</ResourceDictionary>
</sap:ActivityDesigner.Resources>
<DockPanel Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"></ColumnDefinition>
<ColumnDefinition Width="210"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="First Number"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter first number" Expression="{Binding Path=ModelItem.FirstNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Second Number"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter second number" Expression="{Binding Path=ModelItem.SecondNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Result"></TextBlock>
<sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1" OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="The sum of the numbers" UseLocationExpression="True" Expression="{Binding Path=ModelItem.ResultNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=Out, Mode=TwoWay}" />
</Grid>
</DockPanel>
</sap:ActivityDesigner>
The new defined layout for the activity should now look like this:
18. Right-click the activity (in our case ActMathSquareOfSum), and from the Add menu, select Class.... The Add New Item window is displayed.
20. Add the following content in the newly-created DesignerMetadata class:
using MathSquareOfSum;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
namespace ActMathSquareOfSum
{
public class DesignerMetadata : IRegisterMetadata
{
public void Register()
{
AttributeTableBuilder attributeTableBuilder = new AttributeTableBuilder();
attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum), new DesignerAttribute(typeof(MathSqSumDesigner)));
MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());
}
}
}
using MathSquareOfSum;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
namespace ActMathSquareOfSum
{
public class DesignerMetadata : IRegisterMetadata
{
public void Register()
{
AttributeTableBuilder attributeTableBuilder = new AttributeTableBuilder();
attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum), new DesignerAttribute(typeof(MathSqSumDesigner)));
MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());
}
}
}
Make sure you test the project for errors before attempting to build it.
MathSquareOfSum.dll
library is now built and saved in the following location:
%UserProfile%\source\repos\MathSquareOfSum\MathSquareOfSum\bin\Debug
Once the library is built, the Output panel should display a message as depicted below:
- Launch NuGet Package Explorer and click Create a new package (Ctrl + N). A split-window is displayed which shows Package metadata and Package contents. We need to add all dependencies in the latter section.
- Right-click inside the Package contents section. A context menu is displayed.
- Click Add lib folder. Notice a new lib item is created in the Package contents section.
-
Right-click lib and select to Add Existing File….
- Load the external assembly (
.dll
) created above. In our case, theMathSquareOfSum.dll
file is located in the%UserProfile%\source\repos\MathSquareOfSum\MathSquareOfSum\bin\Debug
folder. - With the file selected, access the Edit menu and select Edit Metadata. The left panel is now fitted with editable metadata fields.
- Fill in the fields as you see fit to better describe your custom activity.
-
Fill in the Id field. In our case, it can be UiPath.MathSquare.Activities, where the first value, UiPath, stands for the team which created the package. If no particular team was involved, the first value can be left blank.
- Click the green check mark button in the top-left corner of the Package metadata section to save all changes.
- From the File menu, select Save As.... An explorer window shows up, allowing you to choose the location of the new file.
- Save the file in the folder where local packages are stored by UiPath (
C:\Program Files (x86)\UiPath\Studio\Packages
in versions prior to 2021.4 orC:\Program Files\UiPath\Studio\Packages
starting with version 2021.4). TheUiPath.MathSquare.Activities.1.0.0.nupkg
file is now created in theUiPath.MathSquare.Activities
folder inside the packages folder.
- Access the Package Manager from Studio.
- Since we saved the NuGet package in the local packages folder, the activity should be available in the Local feed.
- Search for the activity and click Install and then Save. The new activity is now installed and can be used in your automation projects.