Search This Blog

Tuesday, July 17, 2007

How to Expose the Work Item Editor Control

I've used this while writing the Cropper TFS Work Item plugin. I needed a way to edit or create a new work item. The WorkItemFormControl located under the Microsoft.TeamFoundation.WorkItemTracking.Controls namespace is the same control used by Visual Studio work item plugin and also by the process template editor (which was the way I found how to use it).

Here's an example for creating a new work item with the control.

First we need to connect to a Team Foundation Server and obtain the WorkItemStore service:

TeamFoundationServer tfs = new TeamFoundationServer(serverName);
tfs.EnsureAuthenticated();
WorkItemStore wis = tfs.GetService(typeof(WorkItemStore)) as WorkItemStore;

After doing so we can now create the WorkItemFormControl:

WorkItemFormControl wifc = new WorkItemFormControl();

The WorkItemFormControl has a string property named FormDefinition. We need to set this property with an XML defining the work item type that we want to use. To do so we use the WorkItemStore service:

WorkItemType wit = wis.Projects[teamProjectName].WorkItemTypes["Bug"];

This will return the the Bug work item type.

After doing so we can use the WorkItemType to set the FormDefinition property:

XmlDocument xmlDocument = wit.Export(false);
wifc.FormDefinition = xmlDocument.InnerXml;

Now we can use the WorkItemFormControl with a newly created work item or an existing one:

WorkItem wi = new WorkItem("Bug"); or WorkItem wi = wis.GetWorkItem(bugId);
wifc.Item = wi;

To show the control embed it to a form or a panel on a form.

4 comments:

Noah said...

VERY helpful, thank you!! This is just what I was looking for. Great explaination.

Anonymous said...

What if I want the newly created wi to show up inside VS2005? (i.e. just like what Team Explorer does when you right-click on it and select to create a wi)

Dudu Shmaya said...

You need to get the DTE2 object which gives you interface to Visual Studio:
DTE2 dte2 = GetService(typeof(SDTE)) as EnvDTE80.DTE2;
Then you can use the following lines:
DocumentService service = (DocumentService)dte2.GetObject("Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService");
WorkItemType wit = (WorkItemType)((ToolStripMenuItem)sender).Tag;
IWorkItemDocument document = service.CreateWorkItem(tfs, wit.Project.Name, wit.Name, this);
service.ShowWorkItem(document);
I'll try to post a full example when I'll have the time.
You can take a look at the sources of http://www.codeplex.com/TFSQueryExplorer.

Anonymous said...

Hello,

Plz share the link if you had posted the example..