Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Programming Team Foundation Server with the TFS SDK : Page 2

Team Foundation Server, Microsoft's offering for software lifecycle management, is both customizable and extensible through the TFS SDK.


advertisement
Accessing Projects Programmatically with the TFS SDK
With the new team project "DevX" successfully added to the Team Foundation Server, you can use the TFS SDK to work with the project programmatically. First, add a web project called WebApplication1 to the Team Project DevX created earlier (see Figure 9).

 
Figure 9. Adding a New Web Project: The dialog shows the process of adding a new web application to the team project named "DevX."

After adding your project, you can check code in and out, view history, etc.

Creating and Reading Work Items from TFS
Now that you have a project in place, you can use the TFS SDK to create and read work items from the project's work item repository. The first step is to add references to the TFS libraries. You can find these assemblies in the C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies folder (see Figure 10).
 
Figure 10. TFS Assemblies: This screenshot of the TFS assembly location shows the DLLs that you may need to reference in your code.
 
Figure 11. Required References: The figure shows the list of assemblies that you must reference to run the sample project.

To work with the TFS SDK, you'll need to add references to the assemblies in Figure 11.

The TFS SDK provides classes and methods for obtaining project information from TFS Server. The following example illustrates how to display various TFS Server details using the SDK:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Collections; using System.Text; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.WorkItemTracking.Client; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { private String tfsServer = "TFSRTM08"; protected void Page_Load(object sender, EventArgs e) { DisplayTFSDetails(tfsServer); } private void DisplayTFSDetails(String tfsServerName) { TeamFoundationServer teamFoundationServer = TeamFoundationServerFactory.GetServer(tfsServerName); teamFoundationServer.Authenticate(); WorkItemStore workItemStore = new WorkItemStore(tfsServerName); Response.Write("<BR>Displaying the Team " + "Foundation Server details<BR>"); Response.Write("<BR>TFS Server Name: " + teamFoundationServer.Name); Response.Write("<BR>TFS Server URI: " + teamFoundationServer.Uri); Response.Write("<BR>Authenticated User Name: " + teamFoundationServer.AuthenticatedUserName); Response.Write("<BR>Authenticated User Display Name: " + teamFoundationServer.AuthenticatedUserDisplayName); } } }

 
Figure 12. TFS Server Details: This output shows TFS Server information requested using the TFS SDK.
When you execute the preceding code, you'll see a window containing the requested information (see Figure 12).

You can easily display a list of all the projects in TFS:

private void ListAllProjects(String tfsServerName) { TeamFoundationServer teamFoundationServer = TeamFoundationServerFactory.GetServer(tfsServerName); teamFoundationServer.Authenticate(); WorkItemStore workItemStore = new WorkItemStore(tfsServerName); foreach (Project pr in workItemStore.Projects) { Response.Write("<BR>Project Name: " + pr.Name); } }

You can call the ListAllProjects method as follows:



ListAllProjects(tfsServer);

 
Figure 13. Project List: You can easily generate a complete list of TFS projects from code.
Figure 13 shows the output.

Adding a Work Item to TFS
The following code snippet illustrates how to add a work item to TFS.

private void WriteToTFS(String tfsServer,String workItemType, String project, Hashtable values) { TeamFoundationServer teamFoundationServer = TeamFoundationServerFactory.GetServer(tfsServer); teamFoundationServer.Authenticate(); WorkItemStore workItemStore = new WorkItemStore(tfsServer); Project tfsProject = workItemStore.Projects["DevX"]; WorkItemType wIType = tfsProject.WorkItemTypes[workItemType]; WorkItem workItem = new WorkItem(wIType); workItem.Title = values["Title"].ToString(); workItem.Description = values["Description"].ToString(); workItem.State = values["State"].ToString(); workItem.Reason = values["Reason"].ToString(); workItem.Fields["Assigned to"].Value = values["Assigned To"].ToString(); ArrayList result = workItem.Validate(); if (result.Count > 0) Response.Write("There was an error adding this work item " + "to the work item repository"); else workItem.Save(); }

Here's an partial example that calls the WriteToTFS method to add a new work item to the work item repository in TFS. Note that it declares several private class-level objects:

private String tfsServer = "TFSRTM08"; private String[] WorkItemTypeStrings = {"Bug","Risk","Task","Others"}; private enum WorkItemCategory {Bug,Risk,Task,Others};

 
Figure 14. New Work Item: The newly added "Bug" work item is highlighted in this figure.
Hashtable values = new Hashtable(); values.Add("Title", "This is a sample bug created using TFS SDK and C#"); values.Add("Description", "This is a sample description"); values.Add("State","Active"); values.Add("Reason", "New"); values.Add("Assigned To", "Administrator"); WriteToTFS("TFSRTM08",WorkItemTypeStrings[ (int)WorkItemCategory.Bug], "DevX",values);
When you execute the preceding code, it creates a new work item of type "Bug." Figure 14 shows the new work item in TFS:



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap