Home > ALM, TFS, TFS2012 > TFS 2012 API: Find all Solutions in Source Control

TFS 2012 API: Find all Solutions in Source Control


In this article, I am going to show how you can retrieve source control to find all solutions. Actually the solution I am provide can find any specific file or pattern. The code supports wildcard search. I AM NOT DOING ANY VALIDATION TO KEEP THE CODE SHORT. If more than one match found, my code will comma separate them. We will use this code in a later post to incorporate it in a Team Build Definition to build all solutions in the Source Control.

Start by Create a WPF Application

Add the following references:

Microsoft.TeamFoundation.Client

Microsoft.TeamFoundation.VersionControl.Client

image

Build the following UI but replace the values in the textboxes with your team project Url and file pattern respectively:

image

The following code is in the code behind


private void Button_Click_1(object sender, RoutedEventArgs e)
{
//Get Team Project collection form Uri
var teamProjectCollection = GetTeamProjectCollection(new Uri(TeamProjectCollectionTextBox.Text));

//Get version control instance from team project collection
var versionControlServer = GetVersionControlServer(teamProjectCollection);

//Get server path for all files with pattern
var pathList = GetPathList(PatternTextBox.Text, versionControlServer);

//Comma separate results
ResultTextBox.Text = string.Join(",", pathList);

}

private IEnumerable<string> GetPathList(string pattern,
VersionControlServer versionControlServer)
{
List<string> solutionList = new List<string>();

//Get items with pattern (must be prefixed with root path (server or local))
// for example $/ or C:\sc
//Version sepc = latest code
// recursionType = Full =  search sub folders
// DeletedState =  only non-deleted files
//ItemType = files
var lists = versionControlServer.GetItems(pattern,
VersionSpec.Latest,
RecursionType.Full,
DeletedState.NonDeleted,
ItemType.File);

if (lists.Items.Any())
{
var list = lists.Items
.Select(i => i.ServerItem)
.ToList();
solutionList.AddRange(list);
}

return solutionList; ;
}

private VersionControlServer GetVersionControlServer(TfsTeamProjectCollection tfsTeamProjectcollection)
{
var versionControlServer = tfsTeamProjectcollection.GetService<VersionControlServer>();

return versionControlServer;
}

private TfsTeamProjectCollection GetTeamProjectCollection(Uri uri)
{
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(uri);

tfs.EnsureAuthenticated();
return tfs;
}

image

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment