Microsoft CRM provides a powerful platform for automating business processes through workflows. Custom workflows allow you to extend the capabilities of out-of-the-box workflows to handle more complex business scenarios. In this article, you will learn how to create custom workflows in Microsoft CRM using a code example.
Prerequisites:
Before you begin, ensure you have the following prerequisites:
- Microsoft CRM 2011 or later
- Visual Studio 2010 or later
- Windows SDK for .NET Framework 4
- Microsoft.CrmSdk.Workflow NuGet package
Creating a Custom Workflow Activity:
Custom workflow activities are reusable components that encapsulate business logic for use in workflows. To create a custom workflow activity, follow these steps:
- Create a Class Library project in Visual Studio.
- Add the Microsoft.CrmSdk.Workflow NuGet package to the project.
- Create a class that inherits from the CodeActivity class.
- Define the input and output parameters for the activity.
- Implement the Execute method, which contains the business logic for the activity.
- Sign the assembly using a strong name key.
- Build the project and deploy the assembly to the CRM server.
Example Code:
Here’s an example of a simple custom workflow activity that sends an email notification when an account record is created:
public class SendAccountNotificationActivity : CodeActivity
{
public new InArgument<Entity> Target { get; set; }
public new InArgument<string> FromAddress { get; set; }
public new InArgument<string> ToAddress { get; set; }
public new InArgument<string> Subject { get; set; }
public new InArgument<string> Body { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
var account = Target.Get(executionContext);
var fromAddress = FromAddress.Get(executionContext);
var toAddress = ToAddress.Get(executionContext);
var subject = Subject.Get(executionContext);
var body = Body.Get(executionContext);
// Send email notification
using (var smtpClient = new SmtpClient())
{
var mailMessage = new MailMessage(fromAddress, toAddress, subject, body);
smtpClient.Send(mailMessage);
}
}
}
Registering the Custom Workflow Activity:
Once you have created your custom workflow activity, you need to register it with CRM so that it can be used in workflows. To register the activity, follow these steps:
- Open the Plugin Registration Tool in CRM.
- Click the New button.
- Select Workflow Activity as the Step Type.
- Enter a name for the activity and select the assembly containing the activity.
- Click OK to register the activity.
Using the Custom Workflow Activity in a Workflow:
After registering the custom workflow activity, you can use it in workflows. To add the activity to a workflow, follow these steps:
- Open the workflow editor in CRM.
- Drag and drop the custom workflow activity onto the workflow canvas.
- Configure the properties of the activity.
- Save and activate the workflow.
Custom workflows provide a powerful tool for automating complex business processes in Microsoft CRM. By creating custom workflow activities, you can extend the capabilities of workflows to handle a wide range of scenarios. The code example provided in this article demonstrates how to create a simple custom workflow activity that sends an email notification.