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.
Reference Assemblies
As with any application that communicates with CRM, you need to reference the CRM SDK assemblies:
CRM 4.0
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;
CRM 2011
using System.Activities;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
Creating Parameters for the Activity
This activity accepts 4 parameters, one of which was required. In this example, we are:
- “Decorating” a Lookup property to show the label “Marketing List” in the Workflow user interface.
- The parameter is required
- Since it is a lookup, the lookup is referencing the list Entity, which is a Marketing List.
CRM 4.0
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);
}
}
!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->!--crlf-->
CRM 2011
In the .NET Framework 4.0, the parameter definition is slightly condensed:
[Input("Marketing List")]
[ReferenceTarget("list")]
[RequiredArgument]
public InArgument<EntityReference> MarketingListEntityReference { get; set; }
!--crlf-->!--crlf-->!--crlf-->!--crlf-->
Class Definition
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.
CRM 4.0
[CrmWorkflowActivity("Remove from Marketing List", "CRM Accelerators")]
public class RemoveFromMarketingList: SequenceActivity
CRM 2011
public class AddToMarketingList : CodeActivity
Execute Method
The Execute method remains the sole method required for a custom workflow activity though the Context parameter and return value have changed.
CRM 4.0
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
CRM 2011
protected override void Execute(CodeActivityContext executionContext)
Connecting to CRM
The code that defines the connection to the CrmService has changed slightly as well, but overall, the concepts remain the same:
- Get the Execution Context
- Get a handle to the Workflow Context or Organizational Service Factory
- Create an instance of the CrmService
CRM 4.0
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService();
CRM 2011
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Accessing Parameter Values
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.
CRM 4.0
Guid ListId = MarketingListLookup.Value;
CRM 2011
Guid ListId = MarketingListEntityReference.Get<EntityReference>(executionContext).Id;
Doing the Work
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.
Registering your Plugin
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.
Conclusion
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.