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

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)




        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;



                this.Dispatcher.Invoke(exe, null);

                GetPathFiles(new_item, keyItem.ServerItem);




    catch (Exception ex)





Download Demo

Add comment
facebook linkedin twitter email

Leave a Reply


  1. guido de jong07/05/2009 ב 16:30

    Woould you know a way to
    1. create a team project programmatically
    (and ensure Sharepoint sites are ok…)

    (the CreateTeamProjectFolder does seem to do everything…)

    2. add new project folders after the
    the team project is created ?
    (to standardize the project structure for our

    much obliged,


  2. shair07/05/2009 ב 17:30


    You can create team project using TFS Power tools with CreateTeamProject Command.

    In TFS2008 changing the Team Project structure isn’t possible.

  3. TarunArora21/06/2011 ב 00:49

    Great series mate!
    The DomainPicker class seems to have been made obsolete, can you show the replacement class and its implementation?
    Cheers, Tarun