Process template customization: prevent any user to change work item state unless the creator

Some day ago I was asked to implement this rule on TFS 2010: “When a work item is in Resolved state, only the creator can close it”.

Before explanation, just two more notes and remarks:

  • I need to implement this rule only for users who use Team System Web Access, not Team Explorer;
  • Only the creator (that is a single specific user) can change the work item state, not a group of users!

TFS 2010 has many features and rules to implement validation on work item states but, unfortunately, none of them fit to resolve my issue. The only solution I found is to create a custom control in order to validate the work item: not just the best solution, but it seems it works.

Let me explain the steps:

  • Create class library project to implement a work item custom control;
  • Extend the FieldControl class and implement the InvalidateDatasource method, where you will implement your validation logic; for example my code is:
WorkItem CurrentWorkItem=this.DataSource as WorkItem;
String currentUser=CurrentWorkItem.Store.TeamProjectCollection.AuthorizedIdentity.DisplayName;
String createdBy = ((Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem)this.WorkItemDatasource).CreatedBy;
if (!String.IsNullOrEmpty(currentUser) && !currentUser.Equals(createdBy)&& isResolvedOrClosed())            
{
  throw new Exception("You are not allowed to update this work item. Only the owner of the activity"+
                     +" can perform this action.");
}

private bool isResolvedOrClosed()
{
 String actualState = ((Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem)this.WorkItemDatasource).State;
 return actualState.Equals("Closed") || actualState.Equals("Resolved");
}
  • Create wicc file like this one:
<?xml version="1.0"?>
 <CustomControl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Assembly>CustomControls</Assembly>
   <FullClassName>CustomControls.OwnerValidationControl</FullClassName>
 </CustomControl>
  • Build the project and copy dll and wicc file into TFS Server under  C:\\Program Files\Microsoft Team Foundation Server 2010\Application Tier\Web Access\Web\App_Data\CustomControls;

While working through this investigation process I’ve found the following resources very useful, and my thanks go to their authors:

Customizing Work Items

Create custom work item control for TFS Web Access 2010 (TWA)

How to use Custom Controls in Work Item Form

Custom Controls in Work Item Types

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s