January 13, 2010

Using VS 2008 Team Foundation Server SourceControl API

Filed under: Development,TFS — Tags: , , , — Tim Lefler @ 1:33 pm

Wanted to build a Visual Studio Web Project that would allow me to select a Team Project from a drop-down box and then list all of the folders associated with the Project.  This will be used as a stepping stone to later add some check in notifications to Team Foundation Server to perform some specific action based on what folder the source member was checked into….

But for now it just lists the folders associated with the project.

So here is HTML associated with my example project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TFSListFolders._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        Select Team Project to Configure:
        <asp:DropDownList ID="drpProjects" runat="server" AutoPostBack="True">
        </asp:DropDownList>
        </div>
    <asp:ListBox ID="lstFolders" runat="server" Height="193px" Width="805px">
    </asp:ListBox>
    </form>
</body>
</html>

Here is the Code Behind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.Server
Imports Microsoft.TeamFoundation.VersionControl.Client
 
Partial Public Class _Default
    Inherits System.Web.UI.Page
 
#Region " Class Module Variables "
    Private sTFSServerName As String = "localhost"
    Private oTFSServer As TeamFoundationServer
    Private oProjects() As ProjectInfo
    Private oTFSSource As VersionControlServer
    Private oProjectSelected As new ProjectInfo
    Private sProjectRoot As String
    Private oStructureService As ICommonStructureService
    Private sConfigFile As String
    Private moConfig As XElement
#End Region
    Protected Sub ConnectTFS()
        ' Get the Team Projects from local TFS
        oTFSServer = TeamFoundationServerFactory.GetServer(sTFSServerName)
 
        ' I'm authenticating with windows credntials because I have
        '		<authentication mode="Windows"/>
        '       <identity impersonate="true"/>  
        ' Set in the Web.config file.  
        ' Otherwise I would have had to specify credntials in the previous call.
        oTFSServer.Authenticate()
 
        ' The Structure service is used later to list the projects.
        oStructureService = CType(oTFSServer.GetService(GetType(ICommonStructureService)), ICommonStructureService)
 
        ' Get the TFS Source Control Object, the sourcecontrol object is used to actually list the folders.
        oTFSSource = CType(oTFSServer.GetService(GetType(VersionControlServer)), VersionControlServer)
    End Sub
 
    Protected Sub LoadPaths()
        Dim thespec As New ItemSpec(sProjectRoot, RecursionType.Full)
        Dim theset As ItemSet = oTFSSource.GetItems(thespec, VersionSpec.Latest, DeletedState.NonDeleted, ItemType.Folder, False)
 
        lstFolders.Items.Clear()
        For Each anItem In theset.Items
            lstFolders.Items.Add(anItem.ServerItem.ToString())
        Next
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Call to local sub to connect to the TFS instance
        ConnectTFS()
        If Not IsPostBack Then
 
            Dim bFirst As Boolean = True
            For Each project In oStructureService.ListAllProjects
                drpProjects.Items.Add(project.ToString)
                If bFirst Then
                    oProjectSelected = project
                    drpProjects.SelectedIndex = 1
                End If
            Next
 
            ' Build the source path of the selected project.
            sProjectRoot = "$/" & oProjectSelected.Name
 
            ' Save off the selected project
            ViewState("sProjectRoot") = sProjectRoot
 
            ' Load the list with the projects, using local sub routine.
            LoadPaths()
        Else
               ' It is a post back so grab the currently selected project
               sProjectRoot = ViewState("sProjectRoot")
        End If
 
    End Sub
 
    Protected Sub drpProjects_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles drpProjects.SelectedIndexChanged
        ' Reconnect the TFS objects since it is post back
        ConnectTFS()
        For Each project In oStructureService.ListAllProjects
            If drpProjects.SelectedValue = project.Name Then
                sProjectRoot = "$/" & project.Name
            End If
        Next
        ' Save off the change
        ViewState("sProjectRoot") = sProjectRoot
 
        ' Display the folders for this project
        LoadPaths()
 
    End Sub
End Class

Here is what the output looks like:

Web Project Display of Team Project Folders

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress