# Writing the code for a custom activity

> 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.

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.

1. Launch Microsoft Visual Studio.
2. Select **File > New >Project…** (shortcut: `Ctrl + Shift + N`). The **New Project** window is displayed.
3. Select **C#** from the **Languages** dropdown menu. The list of all dependencies using C# is displayed.
4. Select **Class Library (.NET Framework)**. This helps us export the custom activity as a `.dll` file. Select **Next** to go to the **Configure your new project** window.

   !["Create a new project" window in Microsoft Visual Studio](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-create-a-new-project-window-in-microsoft-visual-studio-105352-dc74c4c6-73dc0bd7.webp)
5. Fill in the **Project name** field with the desired activity name. In our case, we can use "MathSquareOfSum".
6. Select **.NET Framework 4.6.1** from the **Framework** dropdown menu. This ensures that the library is compatible with UiPath Studio.
   :::important
   In Windows - Legacy projects, UiPath Studio supports activities created with .NET Framework 4.5.2 to 4.6.1. For information on how to migrate activities to .NET for use in projects with the Windows compatibility, see [Migrating Activities to .NET 6](https://docs.uipath.com/sdk/other/latest/developer-guide/migrating-activities-to-net).
   :::

   ![Configuring your new project](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-configuring-your-new-project-105420-0a75d611-05bedfeb.webp)
7. Select **Create** to go to the code designer and start writing the activity code.
8. In the **Solution Explorer** panel, right-click the **References** branch and select **Add Reference...**. The **Reference Manager** window is displayed.
9. Search for the `System.Activities` and `System.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.

   ![Reference Manager window with the System.Activities assembly selected](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-reference-manager-window-with-the-system-activities-assembly-selected-105648-7eff6771-fdca4065.webp)

   ![Reference Manager window with the System.ComponentModel.Composition assembly selected](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-reference-manager-window-with-the-system-componentmodel-composition-assembly-selected-105735-10d923ef-d973f0e2.webp)
10. 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;
    ```

    Our project should now look like this:

    ![How the project looks like after adding the previous codeblock](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-how-the-project-looks-like-after-adding-the-previous-codeblock-105348-0de18cf0-5f0d9fc3.webp)
11. 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)
            {
            }
    ```

    The `<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.

    ![Properties panel of your custom activity](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-properties-panel-of-your-custom-activity-105212-0c5adfcb-7854542a.webp)

    The `<ReguiredArgument>` attribute is needed if you want a declared element to be mandatory for input. If it is not filled in, a caution icon appears in the title bar of the activity.
12. Add the functionality in the `<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);
    }
    ```

## (Optional) Building a Designer Interface

If you do not wish for your activity to have a designer interface, you can skip to the building the library section.
:::important
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.
:::

![Opting for the Window Workflow Foundation component in Visual Studio](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-opting-for-the-window-workflow-foundation-component-in-visual-studio-105688-7a4bae59-7ac20914.webp)

1. Right-click the project from the **Properties** panel (in our case, the project is **MathSquareOfSum**). The context menu shows up.
2. From the **Add** item, select **New Item...**. The **Add New Item** window is displayed.
3. Select **Workflow** under the **Installed** category on the left panel. All related elements are displayed.
4. Select **Activity Designer** and click **Add** to include the item in the project.

   ![Selecting Activity Designer in the Add New Item window.](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-selecting-activity-designer-in-the-add-new-item-window-105716-d7867900-f1e67ea3.webp)

   The **Activity Designer** item is added, and the corresponding `.xaml` file is opened right away. It should look like this:

   ![The .xaml file for the Activity Designer item.](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-the-xaml-file-for-the-activity-designer-item-105228-d1888936-32392622.webp)
5. 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">
       sap:ActivityDesigner.Resources
           <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:

   ![The layout for the activity.](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-the-layout-for-the-activity-105560-7f2ffaf8-ab257dba.webp)
6. Right-click the activity (in our case **ActMathSquareOfSum**), and from the **Add** menu, select **Class...**. The **Add New Item** window is displayed.
7. The **Class** item is already selected. All that is needed now is to rename it to **DesignerMetadata.cs,** and select **Add**. The new class is now added to the activity and opens up in a new tab.

   ![Renaming the Class item to DesignerMetadata.cs.](https://dev-assets.cms.uipath.com/assets/images/sdk/sdk-renaming-the-class-item-to-designermetadata-cs-105660-8ec8507c-93227e9b.webp)
8. 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());
           }
       }
   }
   ```
