Extending Version Control

You can extend Team Foundation version control by accessing and updating items on the server and in a workspace on the local computer.

In this topic

注意

You can also create custom check-in policies and apply them to a team project. For more information, see the following page on the Microsoft Web site: How to: Create Custom Check-in Policies.

Example: Accessing Items on the Server

The following sample uses the VersionControlServer object to list every version of the .xaml files on the server.

To use this example

  1. Create a console application.

  2. Add references to the following assemblies:

  3. Replace the contents of Program.cs (or Module1.vb) with this example.

using System; 
using System.Text; 
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Framework; 
using Microsoft.TeamFoundation.VersionControl.Client; 

namespace VCSample
{
    class Program
    {
        static void Main(string[] args) 
        {
            // Connect to the team project collection, and get the version control server. 
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
               new Uri("https://Server:8080/tfs/DefaultCollection"));
            VersionControlServer vcServer = tpc.GetService<VersionControlServer>(); 

            // List all of the .xaml files.
            ItemSet items = vcServer.GetItems("$/*.xaml", RecursionType.Full); 
            foreach(Item item in items.Items) 
            {
                Console.Write(item.ItemType.ToString());
                Console.Write(": ");
                Console.WriteLine(item.ServerItem.ToString());
            }
        }
    }
}
Imports System
Imports System.Text
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.VersionControl.Client

Module Module1

    Sub Main()
        ' Connect to the team project collection, and get the version control server
        Dim tfsUri As Uri
        tfsUri = New Uri("https://Server:8080/tfs/DefaultCollection")

        Dim tpc As New TfsTeamProjectCollection(tfsUri)

        Dim vcServer As VersionControlServer
        vcServer = tpc.GetService(Of VersionControlServer)()

        ' List all of the .xaml files
        Dim items As ItemSet
        items = vcServer.GetItems("$/*.xaml", RecursionType.Full)
        Dim item As Item
        For Each item In items.Items
            System.Console.Write(item.ItemType.ToString())
            System.Console.Write(": ")
            System.Console.WriteLine(item.ServerItem.ToString())
        Next
    End Sub

End Module

Example: Update Items in a Workspace

You can work with files in a workspace by performing operations such as get, check out, and check in. The following example gets the most recent version of the files in a workspace.

To use this example

  1. Replace the section of code that lists the .xaml files in the previous example with the following code.

  2. Replace the WorkspaceName with the name of the workspace, which is typically the same as the name of the computer that contains the workspace.

  3. Replace UserName with the fully qualified user name of the workspace owner.

For more information, see Workstation.GetLocalWorkspaceInfo and Set Up your Development Machine to Work with your Team's Project.

// Get the workspace that is mapped to c:\BuildProcessTemplate
WorkspaceInfo wsInfo = Workstation.Current.GetLocalWorkspaceInfo(
   vcServer, @"WorkspaceName", @"UserName");
Workspace ws = vcServer.GetWorkspace(wsInfo); 

// Update the workspace with most recent version of the files from the server
GetStatus status = ws.Get();
Console.Write("Conflicts: ");
Console.WriteLine(status.NumConflicts);
' Get the workspace that is mapped to c:\BuildProcessTemplate
Dim wsInfo As WorkspaceInfo
wsInfo = Workstation.Current.GetLocalWorkspaceInfo(vcServer, "WorkspaceName", "UserName")
Dim ws As Workspace
ws = vcServer.GetWorkspace(wsInfo)

' Update the workspace with the most recent version of the files from the server
Dim status As GetStatus
status = ws.Get
Console.Write("Conflicts: ")
Console.WriteLine(status.NumConflicts)

You can also get a workspace that is mapped to a folder on the local computer by passing the full path of that folder to Workstation.GetLocalWorkspaceInfo.

WorkspaceInfo wsInfo = Workstation.Current.GetLocalWorkspaceInfo(@"c:\MyWorkspace");
Dim wsInfo As WorkspaceInfo
wsInfo = Workstation.Current.GetLocalWorkspaceInfo("c:\MyWorkspace")

See Also

Concepts

Extending Team Foundation

Other Resources

Using Version Control