TFS API Part 16: Mapping Source Control Using VersionControlServer
TFS API Part 16: Mapping Source Control Using VersionControlServer
In this post I’ll show how to use VersionControlServer class to map your Source Control.
This post will be the basic start of how to use VersionControlServer class in TFS API.
Download Demo
First add reference for
Microsoft.TeamFoundation
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.Common.dll
Microsoft.TeamFoundation.VersionControl.Client.dll
located in - C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\
Step 1 – Connect TFS + Create VersionControlServer Object
First I create a Domain Project Picker to get Team Foundation Server instance, and after getting tfs instance create new object type VersionControlServer.
private void btn_dp_Click(object sender, RoutedEventArgs e)
{
DomainProjectPicker dp = new DomainProjectPicker(DomainProjectPickerMode.None);
dp.ShowDialog(); //Display DomainProjectPicker
if (dp.SelectedServer != null)
{
tfs = dp.SelectedServer;
//Using ICommonStructureService to get all project in TFS.
ICommonStructureService structureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
ProjectInfo[] projects = structureService.ListAllProjects();
combo_projects.ItemsSource = projects;
//Create VersionControlServer object from TFS
sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
}
}
Step 2 – Get Source Control Files and Folder
To start the mapping I’m sending the starting path of the source control
GetPathFiles(SourceControlView.Items[0] as TreeViewItem, "$/" + path);
//Example: $/ALM-Group
private void GetPathFiles(TreeViewItem TreeItem, String path)
{
try
{
RecursionType recursion = RecursionType.OneLevel;
//Also have - Full, None
Item[] items = null;
// Get the latest version of the information for the items.
ItemSet itemSet = sourceControl.GetItems(path, recursion);
items = itemSet.Items;
foreach (Item keyItem in items)
{
char[] charSeparators = new char[] { '/' };
//Using split to isolated the Project Name and the File Name
string[] ss = keyItem.ServerItem.Split(charSeparators, StringSplitOptions.None);
//!= items[0] ignore the first item, the Team Project Name
if (keyItem != items[0])
{
TreeViewItem new_item = null;
Execute exe = delegate()
{
new_item = new TreeViewItem();
//Get File or Folder Name
string filename = keyItem.ServerItem.Replace(path + "/", string.Empty);
new_item.Header = filename;
TreeItem.Items.Add(new_item);
};
this.Dispatcher.Invoke(exe, null);
GetPathFiles(new_item, keyItem.ServerItem);
}
}
}
catch (Exception ex)
{
}
}
Download Demo