Displaying all branch hierarchies in TFS 2010
If you are utilizing the new TFS 2010 branch folder feature you can easily list the branch hierarchy in source control using the code below:
1: using System;
2: using Microsoft.TeamFoundation.Client;
3: using Microsoft.TeamFoundation.VersionControl.Client;
4:
5: namespace DisplayAllBranches
6: {
7: class Program
8: {
9: static void Main(string[] args)
10: {
11: string serverName = @"https://[SERVERNAME]:8080/tfs";
12:
13: //1.Construct the server object
14: TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(serverName));
15: VersionControlServer vcs = tfs.GetService<VersionControlServer>();
16:
17: //2.Query all root branches
18: BranchObject[] bos = vcs.QueryRootBranchObjects(RecursionType.OneLevel);
19:
20: //3.Display all the root branches
21: Array.ForEach(bos, (bo) => DisplayAllBranches(bo, vcs));
22: Console.ReadKey();
23: }
24:
25: private static void DisplayAllBranches(BranchObject bo, VersionControlServer vcs)
26: {
27: //0.Prepare display indentation
28: for (int tabcounter = 0; tabcounter < recursionlevel; tabcounter++)
29: Console.Write("\t");
30:
31: //1.Display the current branch
32: Console.WriteLine(string.Format("{0}", bo.Properties.RootItem.Item));
33:
34: //2.Query all child branches (one level deep)
35: BranchObject[] childBos = vcs.QueryBranchObjects(bo.Properties.RootItem, RecursionType.OneLevel);
36:
37: //3.Display all children recursively
38: recursionlevel++;
39: foreach (BranchObject child in childBos)
40: {
41: if (child.Properties.RootItem.Item == bo.Properties.RootItem.Item)
42: continue;
43:
44: DisplayAllBranches(child, vcs);
45: }
46: recursionlevel--;
47: }
48:
49: private static int recursionlevel = 0;
50: }
51: }
The result of this code will be something like this:
for a hierarchy like this: