Programing QA

Programing/Developing Questions & Answers. Focus on business application material

Convert WorkItemCollection to ADO.NET DataTable – Turn TFS query result to ADO.NET DataTable.

Very simple to so:

 

Code Snippet
  1. // connect to TFS
  2. System.Net.NetworkCredential cre = new System.Net.NetworkCredential("username", "password", "domain");
  3. TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("http://myserver:8080/MyCollection"), cre);
  4.  
  5. // get the workitem store for selected collection
  6. WorkItemStore store = tfs.GetService<WorkItemStore>();
  7.  
  8. // Query for the work items we want
  9. string wiqlQuery = "Select ID, Title from Issue where (State = 'Active') order by Title";
  10. WorkItemCollection l = store.Query(wiqlQuery);
  11.  
  12. DataTable dt = new DataTable();
  13.  
  14. foreach (WorkItem wi in l) // loop thru all work items in result
  15. {
  16.     DataRow dr = dt.NewRow(); // add new row in datatable foreach work item in result
  17.     
  18.     foreach (Field field in wi.Fields) // loop thru all fields in work item
  19.     {
  20.         // Make sure data table contains the right column and add it if not.
  21.         if (!dt.Columns.Contains(field.Name))
  22.             dt.Columns.Add(field.Name);
  23.  
  24.         string value = string.Empty;
  25.         // make sure value is not null and add it to right data column of the data table.
  26.         if (field.OriginalValue != null)
  27.             value = field.OriginalValue.ToString();
  28.         dr[field.Name] = value;
  29.     }
  30. }

Get all work item match query from team foundation server

Very easy to do:

Code Snippet
  1. // connect to TFS
  2. System.Net.NetworkCredential cre = new System.Net.NetworkCredential("username", "password", "domain");
  3. TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("http://myserver:8080/MyCollection"), cre);
  4.  
  5. // get the workitem store for selected collection
  6. WorkItemStore store = tfs.GetService<WorkItemStore>();
  7.  
  8. string wiqlQuery = "Select ID, Title from Issue where (State = 'Active') order by Title";
  9. WorkItemCollection l = store.Query(wiqlQuery);
  10.  
  11. foreach (WorkItem wi in l)
  12. {
  13.     // Do what ever you need.
  14. }

Get all attributes of TFS work items for specific team collection and their types

This code will get you all the attribute of work items in a specific team collection.

Your might think that you need to loop thru the properties of specific work item type, but not. the attributes are defined in team collection level and this is where we get them from.

Code Snippet
  1. // connect to TFS
  2. System.Net.NetworkCredential cre = new System.Net.NetworkCredential("username", "password", "domain");
  3. TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("http://myserver:8080/MyCollection"), cre);
  4.  
  5. // get the workitem store for selected collection
  6. WorkItemStore store = tfs.GetService<WorkItemStore>();
  7.  
  8. List<KeyValuePair<string, Type>> fields = new List<KeyValuePair<string, Type>>(); // List will hold name and type of the attribute
  9.  
  10. foreach (FieldDefinition field in store.FieldDefinitions) // loop thru all the attributes
  11. {
  12.     fields.Add(new KeyValuePair<string, Type>(field.Name, field.FieldType.GetType())); // add name and type to list.
  13. }

Simple way to get list of all Work Item types in Team Foundation Server project

This code will get all the work item different types if you need them:

Code Snippet
  1. // connect to TFS
  2. System.Net.NetworkCredential cre = new System.Net.NetworkCredential("username", "password", "domain");
  3. TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("http://myserver:8080/MyCollection"), cre);
  4.  
  5. // get the workitem store for selected collection
  6. WorkItemStore store = tfs.GetService<WorkItemStore>();
  7.  
  8. // this list will be used to hold all workitem types
  9. List<string> types = new List<string>();
  10.  
  11. // make sure selected project is there
  12. if (store.Projects.Contains("My Project"))
  13. {
  14.     foreach (WorkItemType type in store.Projects["My Project"].WorkItemTypes) // loop true all it types
  15.     {
  16.         if (!types.Contains(type.Name)) // make sure type not already in list
  17.             types.Add(type.Name); // add it.
  18.     }
  19. }
  20. else
  21.     throw new Exception("No projects in selected collection.");

 

Good Luck.