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);
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.