Angshuman
Angshuman

Reputation: 707

Get a list of team projects using REST api in C# for on premise TFS 2015 Update3

I am trying to get all the projects using REST api call from c# and following the below MSDN documentation:

https://www.visualstudio.com/en-us/docs/integrate/api/tfs/projects

While executing the GetTeamProjects() I am getting the below response:

response {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

I am assuming the error may be due to the authentication type. I am passing Basic while my on premise uses NTLM.

I am trying to get the TFS porjects to get the user permission details.

Upvotes: 0

Views: 2689

Answers (2)

Niel Zeeman
Niel Zeeman

Reputation: 677

I just use this without the need to enable basic authentication:

var client = new WebClient();
client.Credentials = new NetworkCredential("user", "password", "domain");
var response = client.DownloadString("http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2");

Upvotes: 1

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31075

If you have authentication issue, you should get 401 error, not 404. I'm afraid there is something wrong with your code. You can refer to my code below:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetTeamProjectREST
{
    class Program
    {
        public static void Main()
        {
            Task t = GetTeamProjectREST();
            Task.WaitAll(new Task[] { t});
        }
        private static async Task GetTeamProjectREST()
        {
            try
            {
                var username = "domain\\username";
                var password = "password";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Upvotes: 0

Related Questions