I'm trying to programmatically get a list of all the branches in TFS for Visual Studio 2013. After doing some research I found this blog that details how to get the branches:
Displaying all branch hierarchies in TFS 2010
I modified the code to instead store everything in a list.
private void Setup()
{
string serverName = "serverName"; //in the code this is set to the actual server name
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(serverName));
VersionControlServer vcs = tfs.GetService<VersionControlServer>();
var bos = vcs.QueryRootBranchObjects(RecursionType.OneLevel);
Array.ForEach(bos, (bo) => DisplayAllBranches(bo, vcs));
}
private void DisplayAllBranches(BranchObject bo, VersionControlServer vcs)
{
_listOfBranches.Add(bo.Properties.RootItem.Item);
var childBos = vcs.QueryBranchObjects(bo.Properties.RootItem, RecursionType.OneLevel);
foreach (var child in childBos)
{
if (child.Properties.RootItem.Item == bo.Properties.RootItem.Item)
continue;
DisplayAllBranches(child, vcs);
}
}
The problem I'm having is that the BranchObjects[] bos is always empty. Is there something I'm missing or is there a better way to get a list of all the branches?
Can you verify that vcs object is valid? Can you perform some other operation, e.g. getitems? Are you sure you have branches in your repository? Please note that "branch ocjects" were added in tfs 2010 and they arenot equal to just branch operation. In source control explorer they have grey icon, they can be created with context menu
That code should work fine (works for me). I suspect that you are missing your collection name from your server name variable?
If you have a reasonably standard setup, your serverName variable format should be:
http://ServerName:Port/tfs/CollectionName
so for example:http://tfsServer01:8080/tfs/MyCollection
After doing some more research I figured out the problem. Our TFS is structured similarly to the following:
The first level down would not return any Branches because there aren't any. So Instead I used a simpler approach that gets all of the Branches.
It's not the cleanest approach at the moment. But it's able to retrieve all the Branches for the specified project, as well as the Parent Folders (excluding the Root, for now).