Uipath Custom Activity

Creating a Uipath Custom Activity
UiPath Activities are part of the UiPath Studio application and are designed to help you create a clear and smooth automation process. 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:


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.




  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 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.
In the using directive, add the following code:
  • C#
using System.Activities;
using System.ComponentModel;
Write the code for your custom activity. In our case, it needs to look something like this:

  • C#
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.

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.

Comments

Popular Posts