Adding activities out of box to the VS toolbox

In the previous posts, we have already see as to how we can get the custom activities shown up in the toolbox. Either the activity is part of your solution or we use the Choose Toolbox Item dialog.

However, some of our customers want to deliver their custom activities out of box. Their user shouldn't be asked do a Choose Toolbox Item for every activity they want to use. Even one of our internal customer faced the same problem. Below is the step by step approach in how they went about solving the problem:

1. Lets start with out activity which we want to appear in VS out of box. Create the activity. For this sample, we will use a CodeActivity called “MyActivity”

2. Create a Visual Studio Integration Package “MyToolboxItem”

o Add the following two attributes to the package class

 [ProvideToolboxItems(1)]
[ProvideToolboxFormat("CF_WORKFLOW_4")]

o Add the event handlers for “ToolboxInitialized” and “ToolboxUpgraded”. The event handler would use the SVsToolbox service to add the entries to the toolbox. The code looks as follows

 void AddActivityToToolbox(object sender, EventArgs e)
{
       IVsToolbox toolbox = (IVsToolbox)this.GetService(typeof(SVsToolbox));
      if (toolbox != null)
      {
            toolbox.AddTab(CategoryName);
            OleDataObject dataObject = new OleDataObject();
            AssemblyName assemblyName = typeof(MyActivity.MyCodeActivity).Assembly.GetName();
            assemblyName.CodeBase = @"c:\public\ToolboxItemSample\MyActivity\bin\Debug\MyActivity.dll";
            dataObject.SetData("AssemblyName", assemblyName); 
            dataObject.SetData("CF_WORKFLOW_4", "MapperActivity");
            dataObject.SetData("WorkflowItemTypeNameFormat", typeof(MyCodeActivity).AssemblyQualifiedName);
            TBXITEMINFO[] toolboxItemInfo = new TBXITEMINFO[1];
            toolboxItemInfo[0].bstrText = ActivityName;
            Bitmap bmp = new System.Drawing.Bitmap(16, 16);
            toolboxItemInfo[0].hBmp = bmp.GetHbitmap();
            toolbox.AddItem(dataObject, toolboxItemInfo, CategoryName);
       }
}

3. Build the above package and create the registry keys to import by running

"c:\Program Files\Microsoft Visual Studio 2010 SDK\VisualStudioIntegration\Tools\Bin\RegPkg.exe" /codebase /regfile:myActivity.reg /root:SOFTWARE\Microsoft\VisualStudio\10.0 C:\public\ToolboxItemSample\MyToolboxItem\bin\Debug\MyToolboxItem.dll

4. Close all running devenv instances and import the registry settings by running “myActivity.reg”

5. If you now run toolboxdecatur.exe /persistAll, you will see something as in the attached file(RegsitryEntries.txt).

And you are set. Create a new Workflow Project and you will see the new category and the activity in the Toolbox.

In VS 2010, WF toolbox follows the registry model for the out of box activities. Our activities have been registered as well in the registry. Hence, anyone who wants to have the out of box toolbox activity behavior, can do the same using the above. Essentially, we are creating the toolbox item the way VS toolbox understands it using a VS Integration package(Yes – You would need VS SDK). The next step is to extract that information as registry entries and then adding them to the registry hive.

Finally, in order to add the activity on the designer, ensure that either the activity is in the location mentioned above through the code base or by adding an explicit reference to MyActivity.dll or by putting it in GAC.

Hope this helps

Thanks,

Kushal.

ToolboxItemSample.zip