Skip to content

CRM 2011 – Default Name and GroupName for your Custom Activities

August 23, 2011

In CRM 4.0 it was possible to specify the Name and GroupName attributes of Custom Workflow Activities as attributes at compile time. In CRM 2011 these names are set by default by the Plugin Registration Tool to rather unsavoury, not user-friendly values.

If you are deploying plugins repeatedly, or if you want to ensure consistency, it would be nice to be able to provide default values at compile time that, if desired, can be changed during registration. Best of both worlds.

This involves a little modification to your source code and a small modification to the Plugin Registration Tool.

Method

Change the Plugin Registration Tool

The source code for the plugin registration tool is in the CRM 2011 SDK, in the tools/pluginregistration folder. Open the solution, locate the file AssemblyReader.cs and add the highlighted lines of code below in the RetrievePluginsFromAssembly() method, which is halfway through the source file:

(...)
//Checking whether the Custom Activity descends from Activity will save time, because it
//would most likely fail at run-time since the Workflow engine would not know how to handle the class

type = CrmPluginType.WorkflowActivity;
isolatable = CrmPluginIsolatable.No;

// Start modifications
if (t.IsSubclassOf(typeof(System.Activities.Activity)))
{
    foreach (Attribute att in t.GetCustomAttributes(true))
    {
        if (att != null && (att.GetType().FullName == "Gemina.CRM2011.WF.AgCodeActivityAttribute"))
        {
            dynamic attribute = att;
            workflowGroupName = attribute.GroupName;
            pluginName = attribute.Name;
            break;
        }
    }
}
// End modifications

if (t.IsSubclassOf(typeof(System.Workflow.ComponentModel.Activity)))
{
    errorMessage = string.Format("The Custom Workflow Activity {0} class must have the CrmWorkflowActivity attribute set",
        t.FullName);
        (...)

Save and rebuild the tool.

Add the Attribute class to your plugin code

In your plugin project, add a class file called AgCodeActivityAttribute.cs, with the following content:

using System;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.4
 * 22 Aug 2011
 **/
namespace Gemina.CRM2011.WF
{
    [AttributeUsage(AttributeTargets.Class)]
    public sealed class AgCodeActivityAttribute : Attribute
    {

        private string _groupName;
        private string _name;

        public AgCodeActivityAttribute(string name, string groupName)
        {
            this._name = name;
            this._groupName = groupName;
        }

        public string GroupName
        {
            get
            {
                return this._groupName;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
}

Add the Attribute to your Code Activity

Add the attribute to your CodeActivity class, as show below:

    //
    [AgCodeActivity("My Name", "My Group Name")]
    public class MyCustomActivity : CodeActivity
    {
        //
        (...)

You need to add a reference to the (“Gemina.CRM2011.WF“) namespace to the file, as shown below:

//
using System;
using Gemina.CRM2011.WF;
//

Test

If you’ve done everything as described, you should be able to compile your solution, register your plugin with the modified Plugin Registration Tool and check that the Name and GroupName attributes are populated with the default values.

Note that the Plugin Registration Tool will work as usual if you don’t include the new attribute in your plugins.

Alberto “Lazybones” Gemin

Advertisements

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

%d bloggers like this: