Because the
Release Preview Guide February 2012 contains the following statement,
CUSTOM WORKFLOW ACTIVITIES IN CRM ONLINE
With the Microsoft Dynamics CRM Q2 service update, developers can build custom .NET Workflow activity assemblies for Microsoft Dynamics CRM Online. These custom assemblies can be developed and deployed as part of a Microsoft Dynamics CRM solution package. Once a custom workflow activity has been deployed, it can then be utilized within the workflow engine for business process management. These new capabilities ensure parity in the developer experience between Microsoft Dynamics CRM Online and on-premises. It also empowers organizations to bring external data, custom actions and integrations into their standard CRM business processes.
the Custom Workflow Activities could be interesting, also for product development in CRM 2011. Today, the Custom Workflow Activities are running in OnPremise Deployments but not in Online Systems. Because the Microsoft SDK Example is a little too confusing for a first approach, i have decided to create this small examle (based on the SDK-Stuff).
The Problem:
Create a Custom Workflow Activity, that creates a (follow up-)Task after an Account was created. (This is just an example. You can use default workflow functionality to reach this target.)
1. Create Solution with Project
The first step is to create a Visual Studio 2010 Project.
- Select Visual C# -> Workflow -> Activity Library
- Select .NET Framework 4
- Select Name for Solution and Project
- Select Location
 |
Visual Studio Project |
2. Check Project Settings
Check that the Framework-Type is
NOT .NET Framework Client Profile. The correct one is
.NET Framework 4.
 |
Target framework (should be .NET Framework 4) |
3. Create the Class
Delete the automatically created class
Activity1.xaml and create a class called
CreateTask.cs.
 |
The new class |
4. Adding References and Usings
Add references to the following assemblies (Rightclick
References in the project and add...).
- microsoft.xrm.sdk.dll
- microsoft.xrm.sdk.workflow.dll
Add the reference for System.Runtime.Serialization, which is part of the .NET Framework.
 |
Serialization |
(Hint: The
Add References window here is not the default VS2010 one. This one is part of the
Productivity Power Tools. A (MUST HAVE-) Addon for Visual Studio 2010!)
Add the following using-Statements to your class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
 |
References and Usings |
5. Inheritance
Make our class inherit from CodeActivity (System.Activities) and implement the method Execute. You also have to make the class public.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
namespace MK.CRM2011.CustomWorkflowStep {
public class CreateTask : CodeActivity {
}
}
6. Input Parameters
We would like to put a parameter in our Workflow. So every customer can define the subject of the task by it's own.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
namespace MK.CRM2011.CustomWorkflowStep {
public class CreateTask : CodeActivity {
#region Input Properties
[Input("Task Subject")]
[Default("Empty Subject")]
public InArgument<string>TaskSubject{ get; set; }
#endregion
protected override void Execute(CodeActivityContext context) {
throw new NotImplementedException();
}
}
}
The following code has to be added into the Execute-Method.
7. Create Context and Service
The context we need for retrieving the ID of the current record, the service is required for communication with CRM-Webservice (IOrganizationService).
//Create the IWorkflowContext and the
//IOrganizationService for communication with CRM
IWorkflowContext workflowContext =
context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =
context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service =
serviceFactory.CreateOrganizationService(workflowContext.UserId);
8. Business Logic
Now we have to add the logic which does the following.
- Read Text from "TaskSubject" Input Parameter
string newSubject = TaskSubject.Get<string>(context);
Entity newTask = new Entity("task");
- Set subject of the task to TaskSubject value
newTask["subject"] = newSubject;
- Set regardingobjectid of task to PrimaryEntityId of context (this is the id of the account).
newTask["regardingobjectid"] =
new EntityReference("account", workflowContext.PrimaryEntityId)
- Create the new task in CRM
Guid taskId = service.Create(newTask);
9. Sign the assembly
First of all, go to project settings and sign your assembly.
Right Click the Project -> Properties -> Signing -> Sign the assembly -> New -> Enter Key file name "testkey" -> no password (for testing purposes).
 |
Signing |
10. Compile and deploy the assembly
Rebuild your solution and deploy the assembly to the CRM System. This can be done by the PluginRegistrationTool of microsoft. You can find this as part of the current CRM-SDK. You have to compile it by your own. Choose the following settings.
 |
Deployment |