TFS API Part 48 – WorkItemControl And Bypass Work Item Rules

20/02/2013

It’s been a while since I wrote about TFS API, not because there is no more about it is because I worked on Windows Store for the past couple of months so I didn’t had the time.

More About TFS API

So today post will be about two changes in TFS 2012, the first one is WorkItemControl based on WPF that replaced the WorkItemFormControl based on WinForms, the second will be about WorkItemStore bypass rule – Allowing you to save and change work item values without obeying the work item rules.

image

Download Demo Project

Step 1: Connect TFS

First create WPF application using Visual Studio 2012 and connect TFS using TeamProjectPicker API.

private void btnConnect_Click(object sender, RoutedEventArgs e)
{
    var pick = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
    pick.AcceptButtonText = "I want this Team Project!";
    pick.ShowDialog();

    if (pick.SelectedTeamProjectCollection != null)
    {
        _tfs = pick.SelectedTeamProjectCollection;
        //WorkItemStoreFlags - Enum used to determine behavior of work item store object
        _store = new WorkItemStore(_tfs, WorkItemStoreFlags.BypassRules);
        btnShowWits.IsEnabled = true;
    }
    else
    {
        MessageBox.Show("Please select Team Project");
    }
}
 

Step 2: Query Work Items

No just for demo purpose I using the Store object to query all items in the collection.

private void btnShowWits_Click(object sender, RoutedEventArgs e)
{
    var workItems = _store.Query("SELECT [System.Id] FROM WorkItems");

    if (workItems.Count > 0)
    {
        btnShowWit.IsEnabled = true;
        workItemsList.ItemsSource = workItems;
    }
}

Step 3: Bypass WorkItemStore Rules

The interesting part in TFS 2012 that you can create the WorkItemStore object and specify if you want to work in BypassRule mode.

The Bypass Rule allow you to modify the work item fields without any restrictions, for example you can change “Created Date”, or “Created By” – that normally are blocked for changes.

private void chxBypassRules_Checked(object sender, RoutedEventArgs e)
{
    if (_tfs == null) return;
    _store = new WorkItemStore(_tfs, WorkItemStoreFlags.BypassRules);
}

private void chxBypassRules_Unchecked(object sender, RoutedEventArgs e)
{
    if (_tfs == null) return;
    _store = new WorkItemStore(_tfs, WorkItemStoreFlags.None);
}

Step 4: Show Work Item Control

After we specify the WorkItemStore mode we want to work with and have selected a Work Item from the list we want to display him outside Visual Studio using WorkItemControl.

The WorkItemControl needs the Item property set to the work item you want to display (or new work item), then create new Window object and set the WorkItemControl as the new Windows Content.

private void btnShowWit_Click(object sender, RoutedEventArgs e)
{
    if (workItemsList.SelectedItem == null) return;

    _witControl = new WorkItemControl();
    _witControl.Item = (WorkItem)workItemsList.SelectedItem;


    _container = new Window();
    _container.Content = _witControl;

    _container.Show();

    btnShowErrors.IsEnabled = true;
}

private void btnShowErrors_Click(object sender, RoutedEventArgs e)
{
    _witControl.ShowAllFieldErrors("Error Title", 99);
}
Download Demo Project
Enjoy
Add comment
facebook linkedin twitter email

Leave a Reply