I had a task recently that involved automating communication with TFS server. I was rather unpleasantly surprised by lack of information in on line help on this subject. Granted, there were definitions of classes that are used for such task on MSDN, but no examples. Sad situation indeed. After some research, I was able to complete my task. It was indeed a trial and error process. The function below gets the latest from specified branch, using local path and server name as parameters. It only pull list of files from that folder, doing one level recursion.
Disclaimer:
This is a quick and dirty function I put together for my needs:
''' <summary>
''' Populates dataset with list of items from TFS server
''' </summary>
''' <param name="path">Local path to get the latest for such as c:sourcesomefolder</param>
''' <param name="serverName">Server name such as TFSServer</param>
''' <returns>True if no errors are thrown</returns>
''' <remarks></remarks>
Private Function GetFolderHistory(ByVal path As String, ByVal serverName As String) As Boolean
Dim returnValue As Boolean = True
Try
'create server object
Dim server As New TeamFoundationServer(serverName)
' create source control object
Dim version As VersionControlServer = CType(server.GetService(GetType(VersionControlServer)), VersionControlServer)
' limit based on changeset id
changeSetNumber = CInt(txtChangeSet.Text)
' paramters for getting latest call
' list of items to get
Dim itemSpecs As New List(Of ItemSpec)
'only specify local path
itemSpecs.Add(New ItemSpec(path, RecursionType.OneLevel))
'only get the latest
Dim versionSpec As VersionSpec = versionSpec.Latest
' get history data
Dim history As BranchHistoryTreeItem()() = version.GetBranchHistory(itemSpecs.ToArray, versionSpec)
' clear dataset with results
ItemsDataSet.Items.Clear()
Dim name As String
Dim changed As DateTime
Dim changeSetID As Integer
' run through items - 2- dimensional array of history
For counter As Integer = 0 To history.Length - 1
For counter1 As Integer = 0 To history(0).Length - 1
Dim item As BranchHistoryTreeItem = history(counter)(counter1)
' get each items's propeties
name = item.Relative.BranchToItem.ServerItem.Substring(item.Relative.BranchToItem.ServerItem.LastIndexOf("/") + 1)
changed = item.Relative.BranchToItem.CheckinDate
changeSetID = item.Relative.BranchToItem.ChangesetId
' compare to minimim change set id
If changeSetID > changeSetNumber Then
If item.Relative.BranchToItem.ItemType = ItemType.File Then
ItemsDataSet.Items.Rows.Add(New Object() {name, changed, changeSetID})
End If
End If
Next
Next
' update UI
ItemsBindingSource.DataSource = ItemsDataSet
grdItems.Sort(grdItems.Columns(2), System.ComponentModel.ListSortDirection.Descending)
If grdItems.Rows.Count > 0 Then
grdItems.Rows(grdItems.Rows.GetFirstRow(DataGridViewElementStates.None)).Selected = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Script", MessageBoxButtons.OK, MessageBoxIcon.Error)
returnValue = False
End Try
Return returnValue
End Function