How To? Creating Work Item From A Custom Control

10 בדצמבר 2008

תגיות: ,
תגובה אחת

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!

image

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(
    workItem.Project.Store.TeamFoundationServer,
    workItem.Project.Name,
    "Bug",
    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
{
    get
    {
        return workItem;
    }
    set
    {
        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:

docSrv.ShowWorkItem(bugDocument);

This is it. Once we executed the ShowWorkItem method, a new bug work item will be open.

image

When clicking on new bug:

image

Enjoy!

kick it on DotNetKicks.com

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

תגובה אחת

  1. Shai Raiten10 בדצמבר 2008 ב 23:37

    Very Nice Guy!
    Here is more about Creating Work Item:
    How To: Create Generic Work Item Migration Tool For TFS
    More About Creating Work Item Using TFS API

    להגיב