Showing work item form in your app

I hear this question often: How to show work item form in an app? Either an existing workitem, or a new workitem, possibly with certain fields prefilled? This is done as part of samples in SDK, but it is worth a post. It is quite easy and below is the code to do it:

 

    using Microsoft.TeamFoundation.WorkItemTracking.Controls;

    static void ShowWorkItem(WorkItem wi)

    {

        WorkItemFormControl formControl = new WorkItemFormControl();

        formControl.Item = wi;

        formControl.ReadOnly = false;

        formControl.Dock = DockStyle.Fill;

        Form form = new Form();

        form.Text = String.Format("Work item {0}", wi.Id);

        form.Size = formControl.Size;

        form.Controls.Add(formControl);

        form.Show();

    }

 

The WorkItem object passed can be a "new WorkItem()" or existing one. This needs reference to Microsoft.TeamFoundation.WorkItemTracking.Controls.dll, which can be found under devdiv installation folder, usually <drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies.

 

Now the work item form is shown, how to save the changes, show “dirtiness” and validate changes? These can be done directly to WorkItem object. WorkItem.Save() can be called to save it. Use WorkItem.IsDirty to track if user made any changes, and validate its fields using WorkItem.Fields[<index here>].IsValid call.

 

Other controls like result list can also be hosted in other application. To know how, check out WIBrowser sample in VSSDK (https://msdn.microsoft.com/vstudio/extend)