Archive

Archive for April, 2012

TFS SDK: Retreive WorkItems using the Work Item Query Language

Go to the TFS Content Page


To retreive items, you use the Work Item Query Language defined in the TFS SDK.

WIQL is a string like a SQL statement that you pass to the Qeury method of the WorkItemStore. The code below retreive all workitems and display the state and title:

private static void ListItems(WorkItemStore wis)
{
   string select = "Select [State], [Title] From WorkItems";
   WorkItemCollection queryResults = wis.Query(select);
   foreach (WorkItem r in queryResults)
   {
      foreach (FieldDefinition field in queryResults.DisplayFields)
         Console.Write(field.Name + "=" + r[field.Name] + ", ");
      Console.Write(Environment.NewLine);
   }
}

The WIQL is described in detail in the MSDN at http://msdn.microsoft.com/en-us/library/bb130306.aspx

Categories: C#, TFS

TFS SDK: List projects associated to a TFS collection

Go to the TFS Content Page


TFS is structured in a TFS Server -> Collection -> Project hierarchy. A TFS Server can have many collections whichc can also have many projects.

The following code connect ot the TFS Server, list all projects of all collections that exists on the server.

To compile the code below, you will need to add a reference to the following DLL:

   Microsoft.TeamFoundation.Client
   Microsoft.TeamFoundation.Common
   Microsoft.TeamFoundation.WorkItemTracking.Client

You will also need to add:

   using Microsoft.TeamFoundation.Client;
   using Microsoft.TeamFoundation.Framework.Client;
   using System.Collections.ObjectModel;
   using Microsoft.TeamFoundation.WorkItemTracking.Client;
static void Main(string[] args)
{
   TfsConfigurationServer tfsServer = GetConfigurationServer();

   ReadOnlyCollection<CatalogNode> collectionNodes = tfsServer.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);
   foreach (CatalogNode collectionNode in collectionNodes)
   {
      Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
      TfsTeamProjectCollection teamProjectCollection = tfsServer.GetTeamProjectCollection(collectionId);

      ListProjects(teamProjectCollection);
   }
}

static void ListProjects(TfsTeamProjectCollection teamProjectCollection)
{
   WorkItemStore wis = GetWorkItemStore(teamProjectCollection);
   foreach (Project project in wis.Projects)
      Console.WriteLine(project.Name);
}

static WorkItemStore GetWorkItemStore(TfsTeamProjectCollection teamProjectCollection)
{
   return (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));
}

static TfsConfigurationServer GetConfigurationServer()
{
   Uri tfsUri = new Uri("http://tfsserver:8080/TFS");
   return TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
}
Categories: C#, TFS

TFS SDK: Working with Projects, Item types, Items and more, Getting to know the WorkItemStore

Go to the TFS Content Page


You use the WorkItemStore to retreive informations about Work Items of a collection like the projects, item types, items, etc.

To compile the code below, you will need to add a reference to the following DLL:

   Microsoft.TeamFoundation.Client
   Microsoft.TeamFoundation.Common
   Microsoft.TeamFoundation.WorkItemTracking.Client

You will also need to add:

   using Microsoft.TeamFoundation.Client;
   using Microsoft.TeamFoundation.Framework.Client;
   using System.Collections.ObjectModel;
   using Microsoft.TeamFoundation.WorkItemTracking.Client;
static WorkItemStore GetWorkItemStore(TfsTeamProjectCollection teamProjectCollection)
{
   return (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));
}

static TfsConfigurationServer GetConfigurationServer()
{
   Uri tfsUri = new Uri("http://tfsserver:8080/TFS");
   return TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
}
Categories: C#, TFS

TFS SDK: List TFS Collections

Go to the TFS Content Page


The code that follows connect to a TFS Server and list the name of exisintg collections on the server.

To compile the code below, you will need to add a reference to the following DLL:

   Microsoft.TeamFoundation.Client
   Microsoft.TeamFoundation.Common

You will also need to add:

   using Microsoft.TeamFoundation.Client;
   using Microsoft.TeamFoundation.Framework.Client;
   using System.Collections.ObjectModel;
static void ListCollections()
{
   TfsConfigurationServer tfsServer = GetConfigurationServer();

   ReadOnlyCollection collectionNodes = tfsServer.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);
   foreach (CatalogNode collectionNode in collectionNodes)
   {
      Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
      TfsTeamProjectCollection teamProjectCollection = tfsServer.GetTeamProjectCollection(collectionId);
      Console.WriteLine(teamProjectCollection.Name);
   }
}

static TfsConfigurationServer GetConfigurationServer()
{
   Uri tfsUri = new Uri("http://tfsserver:8080/TFS");
   return TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
}
Categories: C#, TFS

TFS SDK: Retreive the user used for the connection to the TFS Server

Go to the TFS Content Page


To retreive the user that was used to connect to the TFS Server, you can call AuthorizedIdentity property on the TfsConfigurationServer.

To compile the code below, you will need to add a reference to the Microsoft.TeamFoundation.Client DLL and add a using Microsoft.TeamFoundation.Client;

static void Main(string[] args)
{
   TfsConfigurationServer tfsServer = GetConfigurationServer();
   Console.WriteLine("You are connected using the user : " + tfsServer.AuthorizedIdentity.DisplayName);
}

static TfsConfigurationServer GetConfigurationServer()
{
   Uri tfsUri = new Uri("http://tfsserver:8080/TFS");
   return TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
}
Categories: C#, TFS

Connect to a Team Foundation Server using the TFS SDK

Go to the TFS Content Page


Before making any calls to the TFS Server, you need to get a reference to a TfsConfigurationServer.

To compile the code below, you will need to add a reference to the Microsoft.TeamFoundation.Client DLL and add a using Microsoft.TeamFoundation.Client;

static TfsConfigurationServer GetConfigurationServer()
{
   Uri tfsUri = new Uri("http://tfsserver:8080/TFS");
   return TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
}
Categories: C#, TFS

TFS: Import changes made in a Work Item Type (WIT) file

Go to the TFS Content Page


Once you have made changes to the WIT file you can import thoses change using the Visual Stusdio Power Tools.

In Visual Studio, click on Tools -> Process Editor -> Work Item Type -> Import WIT.

Select the WIT file that you want to import and select the TFS Project in which you want to import it.

If the Type exists, it will be replaced, otherwise created.

Categories: TFS