Connect TFS Through C#

Create a C# console application from Visual Studio.

Add references to the following assemblies

  • Microsoft.TeamFoundation.Common
  • Microsoft.TeamFoundation.Client

I am using TFS 2013 & Visual Studio 2015 , so local path of DLLs are on following

C:\program files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer

You can use nuget packages also for these dlls

Install-Package Microsoft.VisualStudio.Services.InteractiveClient -Version 14.89.0
Install-Package Microsoft.TeamFoundationServer.Client -Version 14.89.0
Install-Package Microsoft.TeamFoundationServer.ExtendedClient

Just copy paste the following program, and have the TFS reference programically

using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;

namespace TfsApplication
{
    class Program
    {
        static void Main(String[] args)
        {
            var collectionUri = new Uri("http://SERVERIP:8080/TFS/SITECOLLECTIONNAME");
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(collectionUri, new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN"));            
            tfs.EnsureAuthenticated();            
            // your generic code
            
        }
    }
}

TFS Team Project Names through C#

As in previous post, i have mention to Connect the TFS through Client Object Model,

Now in this post i elaborate to find the name of Team Project Name programically through C#

Just simply copy paste the given code in console application it will works


using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Net;

namespace TfsApplication
{
class Program
{
static void Main(String[] args)
{
List<SelectListItem> listTeam = new List<SelectListItem>();
//string lblMessage = “”;//
DataTable tableTeam = new DataTable();
tableTeam.Columns.Add(“Teams”);

List<string> teamPro = ListOFTeamProjectCollections();
teamPro.Sort();
foreach (string TID in teamPro)
{
tableTeam.Rows.Add(TID.ToString());
}
}
}
}

public static List<string> ListOFTeamProjectCollections()

{

try

{

List<string> teamPro = new List<string>();

var collectionUri = new Uri(“http://SERVERIP:8080/tfs/PROJECTCOLLECTION&#8221;);

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(collectionUri, new NetworkCredential(“USERNAME”, “PASSWORD”, “DOMAIN”));

tfs.EnsureAuthenticated();

ICredentialsProvider provider = new UICredentialsProvider();

TfsConfigurationServer configurationServer = tfs.ConfigurationServer;

CatalogNode catalogNode = configurationServer.CatalogNode;

ReadOnlyCollection<CatalogNode> tpcNodes = catalogNode.QueryChildren(

new Guid[] { CatalogResourceTypes.ProjectCollection },

false, CatalogQueryOptions.None);

foreach (CatalogNode tpcNode in tpcNodes)

{

Guid tpcId = new Guid(tpcNode.Resource.Properties[“InstanceId”]);

TfsTeamProjectCollection tpc = configurationServer.GetTeamProjectCollection(tpcId);

tpc.EnsureAuthenticated();

ReadOnlyCollection<CatalogNode> tpNodes = tpcNode.QueryChildren(

new Guid[] { CatalogResourceTypes.TeamProject },

false, CatalogQueryOptions.None);

foreach (CatalogNode tpNode in tpNodes)

{

teamPro.Add(tpNode.Resource.DisplayName.ToString());

}

}

return teamPro;

}

catch (Exception e)

{

}

}

 

Connect TFS Team Project through C#

In order to begin, first you need to create a C# class in your project and add the following code:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;

If you want to connect to the TFS Team Project with hard coded values you can use the following code:

Uri tfsUri = new Uri(http:mytfsuri.comteamCollection);
string teamProjectName = TestProject;
TfsTeamProjectCollection myTfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri);

First you initialize new URI object with your team collection URL which is equal to your TFS URL plus your team collection name. After that, you should create a collection object and initialize it with the URI. You use “GetService” method to initialize the TFS service. Then you can get the team project from it.
Connect to TFS Team Project using TeamProjectPicker Dialog

Collect TFS Settings with TeamProjectPicker Dialog

In order to capture the needed connection information, you can add TeamProjectPicker object to your windows app. You can use its method “ShowDialog” in order to collect the information.

using (projectPicker)
{
var userSelected = projectPicker.ShowDialog();
if (userSelected == DialogResult.Cancel)
{
return;
}
if (projectPicker.SelectedTeamProjectCollection != null)
{
Uri tfsUri = = projectPicker.SelectedTeamProjectCollection.Uri;
string teamProjectName = projectPicker.SelectedProjects[0].Name;
TfsTeamProjectCollection myTfsTeamProjectCollection = projectPicker.SelectedTeamProjectCollection;
}
}