web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Developing Custom Workflow ...

Developing Custom Workflow Activities in CRM 4.0

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

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


Posted in Microsoft Dynamics CRM Tagged: CRM

This was originally posted here.

Comments

*This post is locked for comments