Wednesday 12 September 2012

TFS select work items for project


Yesterday, I needed to connect to get all work items for project in Team Foundation 
Server. I have seen couple guides, but not easy to find.  
 
 
 
Here is how to get WorkItems Collection: 
 
This is example code and is not fit to be implemented as it is. 
 

 
protected WorkItemCollection ConnectToTFS()
{
 WorkItemCollection workItemCollection = null; 
 try
 {
  // lets define location of tfs server
  var tfsServerUri = new Uri("http://localhost:8080/tfs");
 
  // Account definition
  // var password = "password";
  // var domain = "domain";
  // var userName = "user";
var projectName ="TfsQueryProject";
  Project project = null;  
 
  // needed if you access tfs as user
  // NetworkCredential account = new NetworkCredential(userName, password,domain)
// TeamFoundationServer server = new TeamFoundationServer(tfsServerUri , account);

 
  TeamFoundationServer server = new TeamFoundationServer(tfsServerUri );

  // make sure you can
  server.Authenticate();
 
  // Get access to work items collection for server.
  WorkItemStore store = new WorkItemStore(server); 
 
  // get existing project 
  project = store.Projects[projectName]; 
 
  if (project == null)
   throw new Exception("Project could not found"); 
 
  // now construct query for work items that are in project 
  string querySql = "SELECT [System.Id], [System.Title], [System.Description] " +
     "FROM WorkItems " +
     "WHERE [System.TeamProject] = '" + PROJECT + "' " +; 
 
 //Execute query to get collection with have all the workitems from the project specified. 
 workItemCollection = store.Query(querySql); 
 
 

 }
 catch (Exception ex)
 { 
   // Todo:  handle exception as required.
throw ex;
 }
 
return workItemCollection; 
} 

No comments:

Post a Comment