How to Create a Custom Workflow Activity in Ms CRM 2011

Custom Workflow Activity Steps:

 

This topic describes how to create a custom workflow activity, and use it in Microsoft Dynamics CRM.

Required Software and Assemblies

To develop Windows Workflow Foundation 4 custom activities, you must use Microsoft Visual Studio 2010, which requires Microsoft .NET Framework 4. If it is not available on your computer, Microsoft Visual Studio 2010 will install it.

The following assemblies must be added to your project. They can be found in the SDK\Bin folder in Microsoft Dynamics CRM SDK.

  • Microsoft.Xrm.Sdk.dll
  • Microsoft.Xrm.Sdk.Workflow.dll

Use the CodeActivity Workflow Base Class

To create a custom workflow activity, create a class that inherits from the Code Activity

workflow base class. This class is available in the System.Activities namespace. Activities that inherit from the CodeActivity class are able to override the Execute method to produce custom functionality.

To create a custom activity that inherits from CodeActivity

  1. Start Microsoft Visual Studio 2010.
  2. On the File menu, click New, and then click Project.
  3. In the New Project dialog box, select Workflow under Visual C# in the Installed Templates pane, and then select Activity Library.
  4. Specify a name and location for the solution, and click OK.
  5. Navigate to the Project menu and select Properties. On the Application tab, specify .NET Framework 4 as the target framework.
  6. Add references to the Microsoft.Xrm.Sdk.dll and Microsoft.Xrm.Workflow.dll assemblies.
  7. Delete the Activity1.xaml file in the project.
  8. Add a class file (.cs) to the project. In Solution Explorer, right-click the project, select Add, and then click Class. In the Add New Item dialog box, type a name for the class, and click Add.
  9. Open the class file, and add the following using directives:

C#

using System.Activities;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Workflow;

  1. Make the class inherit from the CodeActivity class and give it a public access modifier as shown here:

C#

public class SampleCustomActivity : CodeActivity

  1. Add functionality to the class by adding an Execute method:

C#

protected override void Execute(CodeActivityContext context)

{

    //Activity code

}

 

 .

  1. Specify input and output parameters. For more information,  
  2. Compile the project to create an assembly (.dll).

To view a code sample that demonstrates how to create a custom workflow activity,  

Specify the Name and Group Name for a Custom Workflow Activity

When you register a custom workflow activity assembly, specify the name and group name. The name property specifies the name of the workflow activity. The group name property specifies the name of the submenu added to the main menu in the Microsoft Dynamics CRM process designer. These properties link the custom workflow activity with the Microsoft Dynamics CRM process designer, so the custom activity name will appear in the user interface.

To specify the name and group name for a custom workflow activity, use the PluginType.Name and PluginType.WorkflowActivityGroupName attributes while registering the custom workflow activity assembly. For more information about registering custom workflow activities, If the PluginType.Name and PluginType.WorkflowActivityGroupName attributes are set to null, the custom activity is hidden from the Microsoft Dynamics CRM workflow designer and is only accessible from XAML workflows.

If you are using the Plug-in Registration tool to register the custom workflow activity assembly, you can specify appropriate values in the Name and WorkflowActivityGroupName boxes, under the Editable region. For more information about using the Plug-in Registration tool, see  

After this custom workflow activity is registered, you can use it from the Microsoft Dynamics CRM process designer for workflows or dialogs.  

 

 

Add Metadata to a Custom Workflow Activity

 

The assembly that contains the custom workflow activity definition is annotated using the .NET attributes to provide the metadata that Microsoft Dynamics CRM uses at runtime to link your code to the workflow engine. For more information about .NET attributes,  

Before you start adding metadata to your custom workflow activity definition, ensure that you are aware of the Microsoft Dynamics CRM types and attributes that are supported for the custom workflow activities. For more information.

 

 

Dependency Properties

The first step is to add some properties to your code as shown in the following example. Notice that the declaration is for a dependency property. This indicates that the workflow is dependent on this value. For more information about Windows Workflow Foundation dependency properties

C#

 
public static DependencyProperty aProperty =
DependencyProperty.Register("a",
typeof(CrmNumber),
typeof(MyAddActivity));
 
[CrmInput("a")]
public CrmNumber a
{
get
{
return (CrmNumber)base.GetValue(aProperty);
}
set
{
base.SetValue(aProperty, value);
}
}
 

Adding Input Parameters

While specifying the input parameter in your workflow class, you can also specify a default value for the parameter. The following sample shows the definition of an input parameter.

C#

[Input("DateTime input")]
[Default("2004-07-09T02:54:00Z")]
public InArgument<DateTime> Date { get; set; }
 

This input parameter is annotated with the .NET attribute Input. The  InputAttribute class derives from the ParameterAttribute class, which takes a parameter (ParameterAttribute.Name) to specify the name of the input attribute. This name appears in the process form assistant in the Web application. This lets you map an attribute as an input parameter to the process.

 

 

Adding Output Parameters

Output parameters are added in the same manner as the input parameters. The following sample shows the definition of an output parameter.

C#

[Output("Money output only")]
[Default("23.3")]
public OutArgument<Money> MoneyOutput { get; set; }
 
 

This output parameter is annotated with the .NET attribute Output. The OutputAttribute class derives from the ParameterAttribute class, which takes a parameter (ParameterAttribute.Name) to specify the name of the output attribute. This name appears in the process form assistant in the Web application. This lets you map an attribute as an output.

Adding Input and Output Attributes for the Same Parameter

You can use the input and output attributes for the same parameter. In the following code example, IntParameter is the input as well as the output parameter.

C#

[Input("Int input")]
[Output("Int output")]
[Default("2322")]
public InOutArgument<int> IntParameter { get; set; }
 

Additional Attributes

Some types, such as EntityReference and OptionSetValue, require additional attributes apart from the Input, Output, and Default attributes. The additional attributes are: ReferenceTarget and AttributeTarget. The following sample shows the definition of a parameter of the EntityReference type.

C#

[Input("EntityReference input")]
[Output("EntityReference output")]
[ReferenceTarget("account")]
[Default("3B036E3E-94F9-DE11-B508-00155DBA2902", "account")]
public InOutArgument<EntityReference> AccountReference { get; set; }
 

Adding the Execute Method

Your custom workflow activity must have an Execute method, as shown in the following example.

C#

protected override void Execute(CodeActivityContext context)
{
   if (AccountReference.Get(context).Id != new Guid("3B036E3E-94F9-DE11-B508-00155DBA2902"))
   {
      throw new InvalidPluginExecutionException("Unexpected default value");
   }
}
 

Use the IOrganization Web Service in a Custom Workflow Activity

 To call Microsoft Dynamics CRM organization Web service methods, you must do the following:

  1. Get a reference to IOrganizationServiceFactory.Use the IOrganizationServiceFactory.CreateOrganizationService method to create an instance of IOrganizationService.
  2. Use the IOrganizationService instance to call the supported methods.

Example

The following sample shows how to get the IOrganizationService from within the Execute method of your custom workflow activity.

C#

protected override void Execute(CodeActivityContext executionContext)

{

   // Get the context service.

   IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

 

   // Use the context service to create an instance of IOrganizationService.

   IOrganizationService _orgService = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

  

   // Use _orgService to call methods.

}

 

Advertisements

2 thoughts on “How to Create a Custom Workflow Activity in Ms CRM 2011

  1. I have an entity as application and un change of application status I have to do some calculation. for example, when application status will change as draft to completed mode on update of application entity then I have to calculate investment amount * 15/ 100 and it will display in the commission field. by default in the application draft status mode it will be 0 otherwise there will be value depends on investment amount and application status. Please help me how to do in custom workflow microsoft dynamic crm 2011 and VS 2010.

    Thanks

    Bishnu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s