There’s plenty of documentation available on MSDN and from other sources that discusses how to create custom controls to be used by TFS Work Item Type (WIT) definitions. I was working with a customer recently and I needed to customize a work item to show a button that would launch a custom user interface. The custom UI would create an associated Code Review work item and a shelveset from the user’s current pending changes. I thought there might be an existing implementation of a custom WIT button control but alas, after scouring the web and other resources I couldn’t find any. So I started down the path of writing my own.
As mentioned above there is already a wealth of documentation detailing how to create custom controls, so I won’t get into most of those details. Instead I’ll focus on the code for the button control and how you can re-use the control to add similar functionality to your work item types.
The implementation turns out to be pretty straight forward. I create a new control library and add a new control that derives from UserControl and implements the IWorkItemControl interface. The IWorkItemControl interface implementation is shown below.
With the interface implemented, I then simply add a new button control to the custom control with some default settings as shown.
Finally, I expose the Text and Width properties of the internal button control so that they can be set by any derived controls. I also expose an event that is fired when the button is clicked.
That’s it for the base control, which by itself won’t do much, so there is no *.wicc file necessary at this point.
Now I need to create a derived control that will set the base control’s button text and width accordingly and handle the ButtonClick event to actually perform some work when the user clicks the button. A simple example of this is shown in the next snippet.
With the code in place I create a *.wicc file named MyCustomWitButton and deploy it along with the binaries for my custom control and the base control to Environment.SpecialFolder.CommonApplicationData\Microsoft\Team Foundation\Work Item Tracking\Custom Controls\10.0\ (there are multiple folders which are probed to locate custom WIT controls as explained here). Now I can add the derived button to my Bug work item type definition (no field name or label is required for the button control).
After saving the work item type definition I can now create a new Bug within the team project and I should get the following result.
Finally I can get to the original problem I was trying to solve, which was using a button from the work item form to launch a custom UI to create an associated Code Review work item and shelveset. I created a WPF application and launch it on the ButtonClick event. I am also leveraging the protected _workitem and _serviceProvider members from the base control to access the associated Bug work item and the TFS work item DocumentService respectively. The results are shown below.
I’ve created a project on CodePlex where you can download the source for the base control and a sample derived control.