Experience your
30 day trial
now!
GET STARTED
CRM MVP Mitch Milam is our guest blogger today. Read Mitch’s blog.
I recently upgraded one of my custom workflow activity plugins to CRM 2011 and thought it would be an interesting exercise to walk through that process with you. So let’s walk through the changes, from top to bottom.
As with any application that communicates with CRM, you need to reference the CRM SDK assemblies:
using System.Workflow.Activities; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Compiler; using Microsoft.Crm.Sdk; using Microsoft.Crm.Sdk.Query; using Microsoft.Crm.SdkTypeProxy; using Microsoft.Crm.Workflow;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using System.Activities; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
This activity accepts 4 parameters, one of which was required. In this example, we are:
With CRM 4.0 developers were generally targeting the .NET Framework v3.0 (or3.5), so to define an input parameter looked something like this:
public static DependencyProperty MarketingListLookupProperty = DependencyProperty.Register("MarketingListLookup", typeof(Lookup), typeof(RemoveFromMarketingList));
[CrmInput("Marketing List")]
[ValidationOption(ValidationOption.Required)]
[CrmReferenceTarget("list")]
public Lookup MarketingListLookup
{
get
return (Lookup)base.GetValue(MarketingListLookupProperty);
}
set
base.SetValue(MarketingListLookupProperty, value);
In the .NET Framework 4.0, the parameter definition is slightly condensed:
[Input("Marketing List")]
[ReferenceTarget("list")]
[RequiredArgument]
public InArgument<EntityReference> MarketingListEntityReference { get; set; }
The class definition has changed slightly due to the change in Windows Workflow versions. Beside the base class changing from SequenceActivity to CodeActivity, you will notice the CRM 2011 version does not have the “decorations” that specify the group and name that will be displayed within the CRM workflow editor. More on that later.
[CrmWorkflowActivity("Remove from Marketing List", "CRM Accelerators")] public class RemoveFromMarketingList: SequenceActivity
[CrmWorkflowActivity("Remove from Marketing List", "CRM Accelerators")]
public class RemoveFromMarketingList: SequenceActivity
public class AddToMarketingList : CodeActivity
The Execute method remains the sole method required for a custom workflow activity though the Context parameter and return value have changed.
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
protected override void Execute(CodeActivityContext executionContext)
The code that defines the connection to the CrmService has changed slightly as well, but overall, the concepts remain the same:
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService)); IWorkflowContext context = contextService.Context; ICrmService crmService = context.CreateCrmService();
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Accessing the values found within the input parameters is slightly different in CRM 2011 where the value of the parameter is not actually extracted until you actually need to use it. In CRM 4.0, that process happens more or less automatically due to the DependencyProperty setup.
Guid ListId = MarketingListLookup.Value;
Guid ListId = MarketingListEntityReference.Get<EntityReference>(executionContext).Id;
After you have all of the above code in place, the remainder of your code should function as it did in CRM 4.0. I did not have to make any changes to my code once the “plumbing” was upgraded.
The registration process is exactly the same between versions: You use the Plugin Registration Tool.
The biggest difference between the two is in CRM 2011, the Plugin Registration Tool is where you specify how the custom workflow activity is displayed to the user within the workflow editor.
When you highlight the workflow activity, you need to specify the FriendlyName, Name, and WorkflowActivityGroupName properties as shown below:
Note: Even though the above figure doesn’t show it, the FriendlyName and Name properties need to be the same.
Well, that is about it. It probably only took me 2 hours to perform the upgrade of my code and that was starting from scratch with zero knowledge of the process. I just reviewed the SDK documentation and sample code and worked through each issue as it was encountered.
I hope this helps and good luck.
Mitch Milam
Can I infer from this post that the System.Workflow.Activities.SequenceActivity class from WF3 is no longer supported in crm 2011? And all custom workflow activities that derived from SequenceActivity and were running in crm 4.0 will need to be upgraded using the steps in the post?
WF3 activities are still supported for back-compatibility, but if you want to take advantage of the new workflow activity model you'd need to upgrade them.
Hi, How do tell the workflow that it has successfully finished. In CRM 4 ActivityExecutionStatus.Closed Or ActivityExecutionStatus.Canceling. How do we do this in CRM 2011?