Omid Shariati
Omid Shariati

Reputation: 1916

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>(); 
}

Upvotes: 11

Views: 27105

Answers (2)

Edward Thomson
Edward Thomson

Reputation: 78623

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

Upvotes: 4

Guilherme Oliveira
Guilherme Oliveira

Reputation: 2028

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.

Upvotes: 13

Related Questions