Monday, May 20, 2013

Creating bug work item programatically using TFS 2010 sdk and assign to a user

If you had gone through my previous TFS SDK articles listed below you will be familiar with TFS programming.

http://joymonscode.blogspot.in/2009/05/beginning-tfs-programming.html
http://joymonscode.blogspot.in/2013/04/find-out-name-of-last-checked-in-user.html

As you know using TFS sdk is really simple as knowing how to create the service and basic TFS object model.So what is special about creating TFS bug work item and assign to a developer?

Changing 'Assigned to' field of Work Item object

The speciality is about how you specify the user in the bug. In other words how to set the "Assigned To" field of TFS work item from code? When we list out the bugs in Visual Studio, you might have noticed that the Assigned To column shows the display name. This gives an impression that the TFS is converting the user name to display name for the display purpose. But unfortunately its not.

Hope you know the difference about the username and display name. Username is the unique identification for a user. It didn't allows space in between. But in the display name it allows white spaces as its only for display purpose, not to uniquely identify the user.

Usual user name format - domain\username eg: "companyname\joyg"
Usual display name format - <full name> <last name> eg: "Joy George K"

The point here in creating and assigning TFS bug to a user is, we need to specify the display name in the 'Assigned To' field of WorkItem object instead of the user name. The challenge is most of the other services of TFS returns the username which needs to be converted to Display name before assigning the work item to the user. See the below code to create and assign a work item to a user

        private void AddBug(string userDisplayName)
        {
            WorkItemStore wis = GetWorkItemStore();
            Project tp = wis.Projects[_teamProject];
 
            WorkItemType wit = tp.WorkItemTypes["Bug"];
 
            WorkItem wi = new WorkItem(wit);
            //"[Assigned to] must be display name eg: Joy George K"
            wi.Fields["Assigned to"].Value = userDisplayName; 
            wi.Title = "Bug from TFS sdk demo";
            wi.Description = "Bug from TFS sdk demo";
            ArrayList al = wi.Validate();
            if(al.Count ==0) wi.Save();
        }
        private static WorkItemStore GetWorkItemStore()
        {             TeamFoundationServer tfs = new TeamFoundationServer(new Uri(Path.Combine(_myTFSUri, _teamProjectCollectionName)));             WorkItemStore vcs = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));             return vcs;         }

Get display name from username

As I told earlier, most of the other TFS services return username instead of display name. So it is required to convert username to display name, if you are using the result of other services to create work item against a particular user. Below is the code to convert user name to display name.

        private string GetDisplayNameFromUserName(string userName)
        {
            IGroupSecurityService grpservice = GetGroupSecurityService();
            return grpservice.ReadIdentityFromSource(SearchFactor.AccountName, userName).DisplayName;
        }

        private IGroupSecurityService GetGroupSecurityService()
        {
            TeamFoundationServer tfs = new TeamFoundationServer(new Uri(Path.Combine(_myTFSUri, _teamProjectCollectionName)));
            IGroupSecurityService gss = (IGroupSecurityService)tfs.GetService(typeof(IGroupSecurityService));
            return gss;
        }

Happy coding...

No comments: