【问题标题】:How to access team project list or Git project list using TFS REST API如何使用 TFS REST API 访问团队项目列表或 Git 项目列表
【发布时间】:2016-10-04 22:21:18
【问题描述】:

我正在尝试以下从“本地”TFS 获取项目列表

 private static async void Method()
        {
            try
            {


                using (HttpClient 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://test-test-app1:8080/tfs/boc_projects/_apis/projects?api-version=2").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

我使用的用户名和密码在我尝试连接的 TFS 上具有管理员权限。但是当我尝试上述操作时出现未经授权的访问错误。

【问题讨论】:

  • 我建议使用目录服务而不是 REST API,因为它不需要管理员访问权限。如果你有兴趣,我有代码。
  • 告诉我更多关于目录服务,我不知道

标签: tfs


【解决方案1】:
  1. getting a list of team projects 的 REST API 是:

>

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
  1. 确保您已为您的 TFS 启用基本身份验证:

    • 检查您的 IIS 以查看是否安装了基本身份验证服务角色。
    • 进入 IIS 管理器,选择 Team Foundation Server -- 身份验证 并禁用基本身份验证以外的所有内容。然后做 Team Foundation Server 下的 tfs 节点也是如此。
    • 重新启动 IIS。

【讨论】:

    【解决方案2】:

    这是一个使用目录服务的简单应用。它通过循环遍历所有项目集合和项目来查找文件,并按名称查找文件的实例。根据您的需要更改它不需要太多。

    using System;
    using System.Linq;
    using Microsoft.TeamFoundation.Common;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using Microsoft.TeamFoundation.Framework.Common;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace EpsiFinder
    {
        internal class Program
        {
            // Server URL. Yes, it's hardcoded.  
            public static string Url = @"http://tfs.someserver.com:8080/tfs";
    
        private static void Main()
        {
            // Use this pattern search for the file that you want to find
            var filePatterns = new[] { "somefile.cs" };
    
            var configurationServerUri = new Uri(Url);
            var configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri);
            var configurationServerNode = configurationServer.CatalogNode;
    
            // Query the children of the configuration server node for all of the team project collection nodes
            var tpcNodes = configurationServerNode.QueryChildren(
                    new[] { CatalogResourceTypes.ProjectCollection },
                    false,
                    CatalogQueryOptions.None);
    
            // Changed to use the Catalog Service, which doesn't require admin access.  Yay.
            foreach (var tpcNode in tpcNodes)
            {
                Console.WriteLine("Collection: " + tpcNode.Resource.DisplayName + " - " + tpcNode.Resource.Description);
    
                // Get the ServiceDefinition for the team project collection from the resource.
                var tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"];
                var configLocationService = configurationServer.GetService<ILocationService>();
                var newUrl = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition));
    
                // Connect to the team project collection
                var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(newUrl);
    
                // This is where we can do stuff with the team project collection object
    
                // Get the Version Control instance
                var versionControl = tfs.GetService<VersionControlServer>();
                // Select the branches that match our criteria
                var teamBranches = versionControl.QueryRootBranchObjects(RecursionType.Full)
                                                 .Where(s => !s.Properties.RootItem.IsDeleted)
                                                 .Select(s => s.Properties.RootItem.Item)
                                                 .ToList();
                // Match the file in the branches, spit out the ones that match
                foreach (var item in from teamBranch in teamBranches
                                     from filePattern in filePatterns
                                     from item in
                                         versionControl.GetItems(teamBranch + "/" + filePattern, RecursionType.Full)
                                                       .Items
                                     select item)
                    Console.WriteLine(item.ServerItem);
            }
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2013-05-30
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      相关资源
      最近更新 更多