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

azure-devops, c#, tfs, visual-studio

Solution

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.

Problem

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(@"MeMail@gmail.com", "MyPassword")); WorkItemStore workItemStore = collection.GetService<WorkItemStore>(); } ```

Original source