Accessing TeamFoundationServer source control
This chapter provides an overview of how to access a Microsoft Team Foundation Server (TFS) programmatically using the corresponding DLLs provided by Microsoft Visual Studio®. This documentation was written for a better overview and to simplify the first steps when you try to combine the TwinCAT Automation Interface code with TFS DLLs. For more detailed documentation on the TFS API, we recommend that you refer to the corresponding MSDN articles.
This documentation deals with the following topics:
- Connecting to a TFS server
- Connecting to TeamProjects
- Working with workspaces
- Creating working folders
- Getting the latest versions
- Receiving a list of upcoming changes
- Checking in and out
- Undoing a process
The following TFS API DLLs are used within this documentation:
- Microsoft.TeamFoundation.Client
- Microsoft.TeamFoundation.VersionControl.Client
Connecting to a TFS server
The Microsoft TFS API allows you to connect your application, which contains the TwinCAT code of the Automation Interface, to your development repository on a remote server. The connection requires a string that describes the address, port and composition of your remote server, e.g. http://your-tfs:8080/tfs/DefaultCollection.
Before the connection, you must define an instance of the Uri class and the TfsConfigurationServer class. Assign the remote server link to the configuration server:
Code snippet (C#):
string tfsServerUrl = "http://your-server:8080/tfs/DefaultCollection";
Uri configurationServerUri = new Uri(tfsServerUrl);
TfsTeamProjectCollection teamProjects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(configurationServerUri);
VersionControlServer verControlServer = teamProjects.GetService<VersionControlServer>();Connecting to TeamProjects
You can get a list of team projects or a specific team project on the server by executing the following code snippets:
Code snippet (C#):
// Get all Team projects
TeamProject[] allProjects = verControlServer.GetAllTeamProjects(true);
// Get specific Team Project
TeamProject project = verControlServer.TryGetTeamProject("TeamProjectName");Working with workspaces
Team Foundation works with workspaces that contain mappings to working folders. These mappings link server-side folders with local folders on your hard disk. In addition, the name of the owner and the name of your computer are saved as part of the workspace name. The workspace is a must before performing a version control task as it stores the information about files, versions and the list of pending changes.
To start with TFS Client, the API provides a Workspace class that stores the above information and offers an extensive set of methods to interact with the files and folders. Assuming you want to create a workspace for a folder named folderName, create a string containing the computer name and the folder:
Code snippet (C#):
// Specify workspace name for later use
String workspaceName = String.Format("{0}-{1}", Environment.MachineName, "Test_TFSAPI");
// Create new workspace
Workspace newWorkspace = verControlServer.CreateWorkspace(workspaceName, verControlServer.AuthorizedUser);There can now be a mapping between the server-side folder and your local folder specified by folderName under the workspace. To retain or delete an existing workspace, simply carry out the following methods:
Code snippet (C#):
// Delete workspace
bool workspaceDeleted = verControlServer.DeleteWorkspace(workspaceName, verControlServer.AuthorizedUser);
// Get existing workspace
Workspace existingWorkspace = verControlServer.GetWorkspace(workspaceName, verControlServer.AuthorizedUser);Creating working folders
For a connection to a specified project folder, you need a relative path with regard to the root.
For example, if the project is saved on: your-server\Development\TcSampleProjectX, then the relative path to this folder is $\Development\TcSampleProjectX.
To align this folder with the projects on the server, you can iterate over a set of all registered projects on the server. A registered project is the root of the project collection below it.
Code snippet (C#):
// Create mapping between server and local folder
string serverFolder = String.Format("$/{0}", teamProject.Name + "/Folder/SubFolder");
string localFolder = Path.Combine(@"C:\tfs", workspaceName);
WorkingFolder workingFolder = new WorkingFolder(serverFolder, localFolder);
existingWorkspace.CreateMapping(workingFolder);Getting the latest versions
As mentioned above, the Workspace class provides a rich set of methods to execute TFS commands from code. To illustrate this, here is an example of creating a link to an element in the working folder. To create a relative path:
Code snippet (C#):
String existingItemPath = "$/Developement/TcSampleProject/Examples/Samples00/TestPlc/POUs/MAIN.TcPOU"; Or an absolute path:
String existingItemPath = C:/tfs/Developement/TcSampleProject/Examples/TestPlc/POUs/MAIN.TcPOU";To get the latest version of the element in the example, add the following line of code:
Code snippet (C#):
String existingItemPath = "$/TeamProjectName/Folder/SubFolder";
GetRequest itemRequest = new GetRequest(new ItemSpec(existingItemPath, RecursionType.Full), VersionSpec.Latest);
existingWorkspace.Get(itemRequest, GetOptions.GetAll);Here the RecursionType.Full executes the request for all elements under the element node, but you can choose it according to the requirements of your application and the existing hierarchy of elements. For more information, see the API documentation on MSDN.
Receiving a list of upcoming changes
You can also get a list of pending changes made to the element or a collection of elements using the following line of code:
Code snippet (C#):
PendingChange[] pendingChanges = existingWorkspace.GetPendingChanges(existingItemPath, RecursionType.Full);When collecting elements, take into account the excessive deviations in Item[] as an argument.
Checking in and out
You can check out an element or a collection of elements:
Code snippet (C#):
int checkoutResult = existingWorkspace.PendEdit(existingItemPath, RecursionType.Full);To check in the collection or pending changes to an element:
Code snippet (C#):
int checkinResult = workspace.CheckIn(pendingChanges, usercomment);-Undoing a process
To undo a process, simply do the following:
Code snippet (C#):
int undoResult = existingWorkspace.Undo(existingItemPath, RecursionType.Full);