TFS API Part 19: Merge
TFS API Part 19: Merge
In my last post about TFS API TFS API Part 18: More Basic Stuff On Workspaces I demonstrate how to get / create and delete workspaces.
In this post I’ll show how to perform Merge using TFS API.
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 + Get all project list
private void btn_dp_Click(object sender, RoutedEventArgs e)
{
DomainProjectPicker dp = new DomainProjectPicker(DomainProjectPickerMode.None);
dp.ShowDialog();
if (dp.SelectedServer != null)
{
tfs = new TeamFoundationServer(dp.SelectedServer.Name,new UICredentialsProvider());
tfs.EnsureAuthenticated();
//Using ICommonStructureService to get all project in TFS.
ICommonStructureService structureService = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
ProjectInfo[] projects = structureService.ListAllProjects();
combo_projects.ItemsSource = projects;
sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
AddWorkSapces();
}
}
Step 2 – Add workspaces
In order to perform merge you need workspace to work on.
private void AddWorkSapces()
{
Workspace[] wss = GetWorkspaces();
cob_workspace_list.Items.Clear();
foreach (Workspace ws in wss)
{
WorkSpaceItem item = new WorkSpaceItem(ws.Name, ws);
cob_workspace_list.Items.Add(item);
}
}
public Workspace[] GetWorkspaces()
{
try
{
return sourceControl.QueryWorkspaces(null, sourceControl.AuthenticatedUser, System.Net.Dns.GetHostName().ToString());
//You also can get remote workspaces
//QueryWorkspaces(worspaceName,worksapceOwner,computer)
}
catch
{
throw;
}
}
Step 3 – Map source control folders and files
A simple view to help us select Source folder and Target folder.
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;
//Saving the full path of the file/folder
new_item.Tag = keyItem.ServerItem;
TreeItem.Items.Add(new_item);
};
this.Dispatcher.Invoke(exe, null);
GetPathFiles(new_item, keyItem.ServerItem);
}
}
}
catch (Exception ex)
{
}
}
Step 4 – Perform Merge
Read more about Workspace.Merge
private void Merge(Workspace workspace,string source,string target)
{
GetStatus status = workspace.Merge(source,
target,
null,
null,
LockLevel.None,
RecursionType.Full,
MergeOptions.None);
//public GetStatus Merge (string sourcePath,
// string targetPath,
// VersionSpec versionFrom,
// VersionSpec versionTo,
// LockLevel lockLevel,
// RecursionType recursion,
// MergeOptions mergeOptions)
lbl_NumConflicts.Content = status.NumConflicts.ToString();
lbl_NumFailures.Content = status.NumFailures.ToString();
lbl_NumOperations.Content = status.NumOperations.ToString();
lbl_NumWarnings.Content = status.NumWarnings.ToString();
}
Parameters:
sourcePath - The source of the merge (local or server path).
targetPath - The target of the merge (local or server path -- must be mapped).
versionFrom - The starting version that may be a null reference (
Nothing in Visual Basic).
versionTo - The ending version that may be a null reference (
Nothing in Visual Basic).
lockLevel - The lock level to apply to each item specified by the target.
recursion - The level of recursion desired.
mergeOptions - Specifies the merge options.
AlwaysAcceptMine - discard any changes in source, and just update merge history
Baseless - perform baseless merge
ForceMerge - do not look at merge history and perform merge for specified range of versions
NoMerge - do not perform actual merge
None - no special options

Download Demo Project