Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to TeamFoundationServer (tfs) using client api from a console application?

I'm trying to connect to TeamFoundationServer hosted at visualstudio.com using its client API with a Console Application, but I get this error:

TF400813: Resource not available for anonymous access. Client

My code:

private static void Main(string[] args)
{
    Uri collectionUri = new Uri("https://MyName.visualstudio.com/DefaultCollection");

    TfsTeamProjectCollection collection =
        new TfsTeamProjectCollection(
            collectionUri,
            new System.Net.NetworkCredential(@"[email protected]", "MyPassword"));

    WorkItemStore workItemStore = collection.GetService<WorkItemStore>(); 
}
like image 708
Omid Shariati Avatar asked Feb 19 '14 11:02

Omid Shariati


2 Answers

You have to call the EnsureAuthenticated() method from TfsTeamProjectCollection:

private static void Main(string[] args)
{
    Uri collectionUri = new Uri("https://MyName.visualstudio.com/DefaultCollection");

    NetworkCredential credential = new NetworkCredential("USERNAME", "PASSWORD");
    TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, credential);
    teamProjectCollection.EnsureAuthenticated();

    WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();

    WorkItemCollection workItemCollection = workItemStore.Query("QUERY HERE");

    foreach (var item in workItemCollection)
    {
        //Do something here.
    }
}

I hope it has solved your problem.

like image 151
Guilherme Oliveira Avatar answered Oct 18 '22 19:10

Guilherme Oliveira


Set up alternate credentials for your account. You can use alternate credentials for the command-line clients and as a NetworkCredential parameter.

like image 36
Edward Thomson Avatar answered Oct 18 '22 19:10

Edward Thomson