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 which will take the text as input and return work count as output.
Step 1 - Create New ProjectIn 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 inherited from CodeActivity, it will require you to add System.Activities namespace.
Step 4 - Add Input/Output ParametersOur 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)
- {
- }
- }
- }
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)
- {
- }
- }
- }
Inside Execute method, we are getting 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 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);
- }
- }
- }
In Dynamics 365, it is necessary to sign the assembly before registering. To do this -
- Right click on project, 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...
- Crete 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 bin/debug folder of the project.
After selecting DLL, make sure that Select All is selected in Step 2.
Leave the rest of the options as they are and click Register Selected Plugins. It should register your assembly successfully.
You can verify the assembly after registering in Plugin Registration Tool.
Step 8 - Consuming Custom Workflow Activity in Workflow Process
Open CRM, go to a solution where you want to create Workflow, navigate to Processes & create new. Give some meaningful name ( I've given "Word Counter"), select Category as "Workflow" & select "Contact" in Entity. I have created as a realtime workflow by unchecking "Run this workflow in the background (recommended)" checkbox, for the ease of testing.
After Creation, check "As an on-demand process" to make as On-demand workflow.
Click on "Add Step" and look for our assembly name i.e WordCountWorkflow, click on this WordCountWorkflow.WordCount will appear, click on this to add step.
In added step click "Set Properties", In properties set Input Text (Input parameter given in code) as Address field of the Contact record.
Add another update step, and set Job Title field as Word Count (Output Parameter given in code).
Workflow is completed; save and activate it.
Testing the Workflow
To test this, I have created one contact record with Address field populated which has nine words in total. Expand the top menu in form and click "Run Workflow".
In the popup that appears, select our "Word Counter" workflow and click "Add".
It will ask for confirmation to run, click "OK".
Let the execution complete.
Refresh the form to update and verify whether Job Title field is updated with the Word Count of Address field.
Congrats! Your Custom Workflow Activity is running successfully.
Grab the complete source code here https://github.com/AshV/Custom-Workflows-Dynamics-365.
No comments:
Post a Comment