In Dynamics 365 Workflows, if some functionality is not available in out-of-the-box features, we have the option to write Custom Workflow Activity and consume that in the workflow processes. Let's see how to create Custom Workflow Activities for Dynamics 365. Here, for example, we will write an activity that will take the text as input and return the work count as output.
Step 1. Create a New Project.
In Visual Studio create a new project of type Class Library & select framework version 4.5.2. This might change for future versions. I have given the name as WordCountWorkflow which tells the purpose of the workflow.

Step 2. Add Required Packages.
Go to "Manage NuGet Packages" and install Microsoft.CrmSdk.CoreAssemblies (for Microsoft.Xrm.Sdk namespace) & Microsoft.CrmSdk.Workflow(for Microsoft.Xrm.Sdk.Workflow namespace).

Step 3. Create WordCount Class.
Create class WordCount and inherit from CodeActivity, it will require you to add System. Activities namespace.
Step 4. Add Input/Output Parameters.
Our workflow activity will require one input parameter of type text and one output parameter of type number to return the word count. Let's add them to the class. It would require Microsoft.Xrm.Sdk.Workflow to be added.
using System.Activities;
namespace WordCountWorkflow
{
public class WordCount : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
// Implementation goes here
}
}
}
Step 4. Add Input/Output Parameters 2.
Our workflow activity will require one input parameter of type text and one output parameter of type number to return word count. Let's add them to the class, it would require Microsoft.Xrm.Sdk.Workflow to be added.
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
namespace WordCountWorkflow
{
public class WordCount : CodeActivity
{
[RequiredArgument]
[Input("Input Text")]
public InArgument<string> InputText { get; set; }
[Output("Word Count")]
public OutArgument<int> CountOfWords { get; set; }
protected override void Execute(CodeActivityContext context)
{
// Implementation goes here
}
}
}
Step 5. Add Word Count Logic & Get/Set Parameters.
Inside the Execute method, we are getting the InputText parameter value and applying Split() to split into words then.Lengthproperty to get the count of words. Finally, we are setting this value to the CountOfWords parameter.
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
namespace WordCountWorkflow
{
public class WordCount : CodeActivity
{
[RequiredArgument]
[Input("Input Text")]
public InArgument<string> InputText { get; set; }
[Output("Word Count")]
public OutArgument<int> CountOfWords { get; set; }
protected override void Execute(CodeActivityContext context)
{
this.CountOfWords.Set(
context,
this.InputText.Get<string>(context)
.Split(
new char[] { ' ', '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries
).Length
);
}
}
}
Step 6. Signing the Assembly.
In Dynamics 365, it is necessary to sign the assembly before registering. To do this,
- Right-click on the project, and click on properties to open.
- On the left pane, click on Signing.
- Check Sign the assembly checkbox.
- In Choose a strong name key file dropdown click New...
- Create Strong Name Key popup will appear.
- Give some name.
- Optionally, you can protect this key file with a password.
- Click OK to generate the key and sign the assembly.
- Build the solution.
Step 7. Register the Assembly in Dynamics 365.
Open the Plugin Registration Tool and connect with your organization. If you don't already have it, grab it by adding Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool NuGet Package.
Click on Register >> Register New Assembly.
The "Register New Assembly" popup will appear; select your project DLL from the bin/debug folder of the project.
After selecting DLL, make sure that Select All is selected in Step 2.


















Mark HardmanPosted Aug 27, 2020, 3:27 AM
Awesome - very helpful. Just as a hint, be careful to use correct target for .net runtim (as of 27/08/2020 I had to use 4.6.2, not 4.7.2)
Utsav UpadhyayPosted Aug 13, 2020, 5:27 AM
Actually i want class library to return json and i can call this workflow from any plug-in and that plug-in would use response of this class library.
Utsav UpadhyayPosted Aug 13, 2020, 5:26 AM
What is the use of XRm.Workflow namespace here ?
murali krishnaPosted Jan 16, 2020, 10:00 AM
Thank you so much sir........
murali krishnaPosted Jan 11, 2020, 11:01 PM
Sir...can you send a real time schenarios..?
murali krishnaPosted Jan 11, 2020, 10:59 PM
What is the use of word count ..?
Phil LivermorePosted Aug 23, 2018, 9:24 AM
You have one of these that updates child records ?