I have created for one of my customers, a work item that represents a Test. Within this Test work item, I have created a custom control that implements a Test Story, Test Case, Test Step, Test Run and so on and so forth.
In this post I am not going to go over the Test work item I have created because it deserves a post on its own. For now I will show you a screen shot and will say that it is super cool!
So back to the post in hand, I had to create a custom control. This custom control needed the capability to create a new work item of type Bug, and open it within visual studio.
How To create a work item from a custom control?
First we need to user the IServiceProvider instance that we get when we create a custom control. Since every custom control must inherit from IWorkItemControl it should implement a method:
void SetSite(IServiceProvider serviceProvider);
In this method we get this IServiceProvider instance and we keep it, for example:
public void SetSite(IServiceProvider serviceProvider)
this.serviceProvider = serviceProvider;
Where serviceProvider is a field within the custom control. The next thing we need to do is to create a Document Service that will enable us to create a Visual Studio document to populate a work item:
DocumentService docSrv = (DocumentService)serviceProvider.GetService(typeof(DocumentService));
So, now lets create the document:
IWorkItemDocument bugDocument = docSrv.CreateWorkItem(
this) as IWorkItemDocument;
As you can see, I have used the workItem to create a work item document. This is a field we have when we implement the IWorkItemControl:
public object WorkItemDatasource
workItem = value as WorkItem;
Now we have a document ready and all we have left to do is open the document and show it. In order to do so I use the following code:
This is it. Once we executed the ShowWorkItem method, a new bug work item will be open.
When clicking on new bug: