UiPath Activities Guide

Creating a Custom Activity

An activity is the building block of a process automation. UiPath Studio comes with various built-in Core Activities, as well as dedicated activities (PDF, mail, Excel) you can install through the Package Manager, depending on your needs. You can read The UiPath Activities Guide for more details and examples, as well as how to Manage Activities Packages. Additionally, you can create custom activities to better automate processes based on your needs.

There are two major steps required to create a custom activity:

  1. Writing the custom activity code.
  2. Adding the external assembly (.dll) in UiPath.

Note that the following components are required to create a custom activity:

You can write the code for your custom activity either deriving from CodeActivity or NativeActivity. The two activities look rather similar, but there are differences. You can view an example of a detailed NativeActivity here.

CodeActivity provides an Execute method that can be overridden to have your implementation. It also provides access to tracking, variables, and arguments. You can use the CodeActivity for the following:

  • Simple custom activities.
  • Synchronous activities.
  • Execution in a single pulse of execution.
  • Schedule other activities.
  • Use advanced WF Runtime features.

NativeActivity provides all the features of the CodeActivity, but also lets you abort the execution of the activity, cancel child activity execution, schedule activities, use bookmarks, as well as activity actions and functions. You can use the NativeActivity for the following:

  • Complex custom activities.
  • Running long custom activities.
  • Execution in multiple pulses of execution.
  • Schedule other activities.
  • Use advanced WF Runtime features.

Note:

Only use CodeActivity to create activities that do not require capabilities of WF Runtime.

Writing the Custom Activity Code

To better understand how to write the code for a custom activity, we are going to create an activity which asks the user for two numbers, then outputs the square of their sum.

Note:

In order to write the code for your custom activity you need to install Microsoft Visual Studio with the .NET desktop development workload.

  1. Launch Microsoft Visual Studio.
  2. Click File > New >Project… (shortcut: Ctrl + Shift + N). The New Project window is displayed.
  3. Click Visual C#. The list of all dependencies using C# is displayed.
  4. Optionally, fill in the Name field with a preferred name for your custom activity. In our case, we can use “MathSquareOfSum”
  5. Select Class Library (.NET Framework) and click OK. This helps us export the custom activity as a .dll file.
  6. Click Project > Add Reference….
  7. Search for the System.Activities and System.ComponentModel.Composition references and select them.
  8. Click the OK button.This makes it possible to use classes from the System.Activities and System.ComponentModel.Composition references.

Only now do you decide whether you use the CodeActivity or the NativeActivity. In this example we use the CodeActivity.

  1. In the using directive, add the following code:
using System.Activities;
using System.ComponentModel;
  1. Write the code for your custom activity. In our case, it needs to look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Activities;
using System.ComponentModel;

    namespace ClassMathCustomActivity
{
    public class SimpleFormula : CodeActivity
    {
        [Category("Input")]
        [RequiredArgument]
        public InArgument<double> FirstNumber { get; set; }

        [Category("Input")]
        public InArgument<double> SecondNumber { get; set; }

        [Category("Output")]
        public OutArgument<double> ResultNumber { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            var firstNumber = FirstNumber.Get(context);
            var secondNumber = SecondNumber.Get(context);
            var result = System.Math.Pow(firstNumber + secondNumber, 2);
            ResultNumber.Set(context, result);
        }
    }
}

Note:

We used the protected override void Execute(CodeActivityContext context) code to override the execute method.

Click Build > Build MathSquareOfSum.The Output panel is displayed, informing you that the file is built and displays its path. In our case the MathSquareOfSum.dll file is created.

Adding the external assembly (.dll) in UiPath

Creating the NuGet Package

Before the external assembly can be added in UiPath, a NuGet package first needs to be created.

Note:

In order to create a NuGet package you need to install NuGet Package Explorer.

  1. Launch NuGet Package Explorer.
  2. 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.
    3.Right-click the Package contents section. A context menu is displayed.
  3. Click Add lib folder. Notice a new lib item is created in the Package contents section.
  4. Right-click lib and select to Add Existing File….
  5. Load the external assembly (.dll) created above . In our case, MathSquareOfSum.dll.
  6. Click Edit > Edit Metadata. The Package metadata section is displayed.
  7. Fill in the fields as you see fit to better describe your custom activity.
  8. Fill in the Id field. In our case, it can be “ActivitiesCustomMathFunction”.

Important!

The NuGet Package Id field must contain the keyword “Activities” to appear in the Manage Packages window, in Studio.

  1. Click File > Save. In our case, the ActivitiesCustomMathFunction.1.0.0.nupkg file is created.
  2. Copy the file inside the Packages folder of your UiPath Studio install location (%USERPROFILE%\.nuget\Packages).The NuGet Package containing your custom activity is now ready to be loaded in UiPath Studio.

Note:

Be sure to create an intuitive folder structure for your activities. All empty folders inside the custom activity get removed when used with Orchestrator.

Loading the NuGet Package in UiPath Studio

Once an activity is created and packed in a .nupkg file, installing it in Studio is done similarly to other activities. Learn more about Adding your own Packages.

Creating a Custom Activity