We can create our own custom workflow activities and register it so that they can appear inside workflow editor in CRM.
For creating custom workflow activity we need to first select Workflow Activity Library project template inside Visual Studio with framework version 3.0.
Class within the activity library should derive from Activity class or could derive from SequenceActivity class.
Let’s create a simple custom workflow class
For this add a class inside your Workflow Activity Library project.
Add reference to Microsoft.Crm.Sdk and Microsoft.Crm.SdkTypeProxy dlls.
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.Crm.Workflow;
namespace MyWorkflow
{
[CrmWorkflowActivity(“My First Custom Activity”,“My Custom Activity Group”)]
public class MySimpleCustomActivity :Activity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
// custom logic
return ActivityExecutionStatus.Closed;
}
}
}
CrmWorkflowActivity Attribute – To allow our activity to be visible inside the workflow editor. First parameter is the name of the workflow activity and second parameter is the group name, which could be used for grouping similar kind of workflow activities.
We can create custom activity by inheriting SequenceActivity class, which would be a composite activity wherein we can add other activity available with windows workflow foundation like
IfElseActivity,IfElseBranchActivity,PolicyActivity,DelayActivity etc.
For this right click on your project and add a new Activity in your project.
Open it in design mode. We can see the option of (Drop Activities Here)
Therein we can drop the built in activities found in windows workflow foundation for building our custom logic. Here we need not override the Execute method like we did in earlier case. The sequence activity will execute the activities that we would have dragged and dropped for writing our logic.
[CrmWorkflowActivity(“My First Custom Activity”, “My Custom Activity Group”)]
public partial class Activity3: SequenceActivity
{
public Activity3()
{
InitializeComponent();
}
}
While building our workflow we could also define input and output parameter that will appear inside Set Properties dialog box while defining Step and Dyanmic Value editor inside Form Assistant respectively.
To define input and output parameters we need to make use of dependency property and CrmInput and CrmOutput Attribute.
[CrmInput(“Name”)]
[CrmDefault(“Nishant”)]
public string InputProperty
{
get { return (string)GetValue(InputPropertyProperty); }
set { SetValue(InputPropertyProperty, value); }
}
public static readonly DependencyProperty InputPropertyProperty =
DependencyProperty.Register(“InputProperty”, typeof(string), typeof(nameOfActivityClass));
Similary we can define output parameter only difference would that we will use [CrmOutput(“FullName”)] attribute.
CrmDefault attribute is optional one.
The same property could act as both input as well as output parameter.
[CrmInput(“FName”)]
[CrmOutput(“LName”)]
[CrmDefault(“Nishant”)]
public string MyProperty
{
. . . .
The following data type are supported that could be used as input/output parameter
String, CrmNumber, CrmDecimal, CrmFloat, CrmMoney , CrmDateTime, CrmMoney, CrmBoolean, Status, Picklist, Lookup.
For lookup we can specify the parameter as following
[CrmInput(“Territory”)]
[CrmReferenceTarget(“territory”)]
public Lookup TerritoryProperty
{
get { return (Lookup)GetValue(TerritoryPropertyProperty); }
set { SetValue(TerritoryPropertyProperty, value); }
}
public static readonly DependencyProperty TerritoryPropertyProperty =
DependencyProperty.Register(“TerritoryProperty”, typeof(Lookup), typeof(Activity1));
For picklist
[CrmInput(“AddressType”)]
[CrmAttributeTarget(“account”,“address1_addresstypecode”)]
public Picklist AddressTypeProperty
{
get { return (Picklist)GetValue(AddressTypePropertyProperty); }
set { SetValue(AddressTypePropertyProperty, value); }
}
public static readonly DependencyProperty AddressTypePropertyProperty =
DependencyProperty.Register(“AddressTypeProperty”, typeof(Picklist), typeof(Activity1));
Using CrmService and MetadataService within custom workflow activity through workflow context
We can register our own custom services with workflow runtime. The built in services provided by Windows workflow foundation are following
PersistenceService, QueuingService, SchedulerService, TransactionService, TrackingService etc.
Similary MS CRM has used it to register ContextService with the runtime. It provides the current context information for the workflow instance which is executing at that time.
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService();
IMetadataService crmMetadataService = context.CreateMetadataService();
return ActivityExecutionStatus.Closed;
}
For registering the custom activity we need to use Plugin registration tool.
Open Plugin Registration Tool
Select Register New Assembly
Select you assmebly ( It should be signed assembly and no need to specify anything else within the plugin registration tool).
Click on Register Selected Plugin
Restart Crm Asynchronous Service
Bye..
hi Nishant
i have to create different types of attributes on run time. means i have to create five check box attribute and 3 drop down list , or 2 radio button attribute. what is the most possible way to achieve this. and these attribute change dynamically means one time i need 2 radio button another time i need 3 radio button can u tell me how i will achieve this task please send me help on my email id
vivekmishra12345@gmail.com
thanks
vivek
LikeLike
great article! I just getting into WF and was looking for clarification on Activity vs SequenceActivity. Thanks!
LikeLike
Hi Nishant. Quick question, relatively new to developing in the CRM SDK. Where did you find the list of supported input / output types in the SDK?
I wanted to output an array of activityparty[], but this does not seem to be supported. I was trying to look through SDK chm file but could not find list of supported output types, if you try output an array it gives error when tyring to associate that custom workflow activity to a CRM workflow.
Thanks,
Gaurav
LikeLike
Ya I think we can’t create an array for that! Within sdk you could find this section “Attributes and microsoft dynamcis crm type” within workflows that has some information regarding the same !
LikeLike
Hi,
I want to know if i can use window workflow foundation component, like “Delay” or “Listen” for create my own custom activity “wait condition” in CRM 4.0 Workflow activity.
If yes :
-Can a Workflow activity has provided a timeout? My “custom wait condition” may be waiting a whole year before continuing the treatment
-What happen with recovery if server crash? Is it like “out of the box CRM wait condition”?
-What is the impact for the server performance and async service if i have a lot “custom wait condition” in memory?
Also, how I can get the current context information for the workflow instance if I not override the Execute method??
Thanks!
LikeLike
Any answer for me??
Thanks
LikeLike