TFSExamples.com
Navigation
Main Page
Random Page
Create a new Page
All Pages
Categories
Administration
File Management
Login/Logout
Language Selection
Your Profile
Create Account
Quick Search
Advanced Search »
Back
History
Connect to TFS
===Connect to TFS=== When connecting to a Team Foundation Server, there are two basic approaches using the API: # '''[http://msdn2.microsoft.com/en-us/library/microsoft.teamfoundation.client.teamfoundationserverfactory.aspx|TeamFoundationServerFactory].[http://msdn2.microsoft.com/en-us/library/microsoft.teamfoundation.client.teamfoundationserverfactory.getserver.aspx|GetServer()]''' - this approach will return a cached instance of TeamFoundationServer if one matching the TFS URL is found. You would typically use this approach if you need to make several API calls using the same TeamFoundationServer instance. # '''[http://msdn2.microsoft.com/en-us/library/microsoft.teamfoundation.client.teamfoundationserver.aspx|TeamFoundationServer] constructor''' - this approach allows you to create multiple instances of [http://www.resumesplanet.com|resume writing services] TeamFoundationServer using varying sets of credentials. You would typically use this approach if you need to make multiple API calls against the same Team Foundation Server with different credentials. ====Example Code==== =====Example 1 - Connect Using Cached Instance===== <code c#>// Obtain cached instance of TeamFoundationServer (if a match is found) TeamFoundationServer tfServer = TeamFoundationServerFactory.GetServer(tfServerName); // Ensure the current user can authenticate with TFS - an exception is raised if the credentials // cannot be authenticated or if permission is not allowed tfServer.EnsureAuthenticated(); </code> {BR} =====Example 2 - Connect Using Cached Instance with Ability to Prompt for Credentials===== <code c#>// Obtain cached instance of TeamFoundationServer (if a match is found). If the current credentials are not // valid, then a connection dialog will be displayed when EnsureAuthenticated is called below TeamFoundationServer tfServer = TeamFoundationServerFactory.GetServer(tfServerName, '''new UICredentialsProvider()'''); // Ensure the current user can authenticate with TFS tfServer.EnsureAuthenticated(); </code> {BR} =====Example 3 - Connect by Creating a New Instance===== <code c#>// Create a new instance of Team Foundation Server using the current credentials TeamFoundationServer tfServer = new TeamFoundationServer(tfServerName); // Ensure the current user can authenticate with TFS - an exception is raised if the credentials // cannot be authenticated or if permission is not allowed tfServer.EnsureAuthenticated(); </code> {BR} =====Example 4 - Connect by Creating a New Instance using Custom Credentials===== <code c#>// Create a new instance of Team Foundation Server using custom credentials. TeamFoundationServer tfServer = new TeamFoundationServer(tfServerName, new NetworkCredential("User", "Password", "Domain")); // Ensure the specified user can authenticate with TFS tfServer.EnsureAuthenticated(); </code> {BR} =====Example 5 - Connect by Creating a New Instance with Ability to Prompt for Credentials===== <code c#>// Create a new instance of Team Foundation Server using the current credentials. If the current // credentials are not valid, then a connection dialog will be displayed when EnsureAuthenticated // is called below TeamFoundationServer tfServer = new TeamFoundationServer(tfServerName, '''new UICredentialsProvider()'''); // Ensure the current user can authenticate with TFS tfServer.EnsureAuthenticated(); </code> {BR} =====Example 6 - Connect by Creating a New Instance using Custom Credentials with Ability to Prompt for Credentials===== <code c#>// Create a new instance of Team Foundation Server using custom credentials. If the custom // credentials are not valid, then a connection dialog will be displayed when EnsureAuthenticated // is called below TeamFoundationServer tfServer = new TeamFoundationServer(tfServerName, '''new NetworkCredential("User", "Password", "Domain"), new UICredentialsProvider()'''); // Ensure the specified user can authenticate with TFS tfServer.EnsureAuthenticated(); </code> ===Notes and Resources=== [http://blogs.msdn.com/buckh/default.aspx|Buck Hodges] has a couple of blog posts containing further details regarding the above APIs: {BR}{BR} * [http://blogs.msdn.com/buckh/archive/2006/09/14/factory-vs-constructor.aspx|How to choose between TeamFoundationServerFactory.GetServer() and the TeamFoundationServer constructor] * [http://blogs.msdn.com/buckh/archive/2006/03/17/credentialprovider.aspx|How to get the login dialog when using the Team Foundation Server API]
ScrewTurn Wiki
version 2.0.36. Some of the icons created by
FamFamFam
.