Friday, May 22, 2009

Beginning tfs programming

Some readers may not have knowledge about the new project management framework introduced by Microsoft.So let’s start from what is Team Foundation Server.

What is tfs
Simply saying tfs is a replacement for the existing source control mechanisms such as VSS.But it has got so many additional features like bug tracking,bug automation,reporting etc... In earlier days we have use different tools to do these tasks such as VSS for source control,OnTime for bug tracking etc…Now everything  under one umbrella.So it is more like a project management environment.See what wikipedia says about tfs.

WorkItems contains all the bugs,feature request along with all other tasks.The main advantage of tfs which I have seen is we can associate a source file check in with a bug id easily.

Introduction to Team Foundation Server programming.
Another big advantage of tfs is it’s programming interface.We can easily develop programs which access tfs.We can create work items,log bugs view work items etc…
Microsoft has provided .net libraries for working with tfs.Those dlls normally reside in the folder
[Install drive]:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies
Most of the tfs related dlls have name starting with “Microsoft.TeamFoundation”. Here are the commonly used tfs dlls in developing applications.

  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll

Authenticating into tfs server
Like any other source control system tfs too stores files and data in a remote server and it needs authentication to that server before accessing tfs resources.Here is the code which authenticate a user into tfs system from our application.

NetworkCredential tfsCredential = new NetworkCredential(login, password);
TeamFoundationServer tfs = new TeamFoundationServer(tfsName, tfsCredential);
tfs.Authenticate();

Here login is the login id of the user and tfs name is the full url of the tfs server.eg https://mytfs.mycompany.com:443/
Once the user is authenticated we can query work items as well.

Listing all projects of user in tfs


//Connecting Server
NetworkCredential tfsCredential = new NetworkCredential(login, password);
TeamFoundationServer tfs = new TeamFoundationServer(tfsName, tfsCredential);
tfs.Authenticate();

WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

//Iterate Through Projects
foreach (Project project in wis.Projects)
{
Console.WriteLine(project.Name);
lvProjects.Items.Add(new ListViewItem() { Text = project.Name });
}


No need to explain I think.Just adding all the projects into a ListView.

Listing work items in a Project


private void LoadWorkItems(Project project)
{
WorkItemCollection wic = project.Store.Query(
" SELECT [System.Id], [System.WorkItemType]," +
" [System.State], [System.AssignedTo], [System.Title] " +
" FROM WorkItems " +
" WHERE [System.TeamProject] = '" + project.Name +
"' ORDER BY [System.WorkItemType], [System.Id]");
foreach (WorkItem wi in wic)
{
lbWorkItems.Items.Add(wi);
}
}


Again a self explanatory code.Adds all work items into another list box.


A WIME (Works In My Environment) sample is available here.

2 comments:

Blogger said...
This comment has been removed by a blog administrator.
Blogger said...
This comment has been removed by a blog administrator.