c# - 尝试通过客户端库获取 TFS 用户列表

标签 c# tfs .net-core azure-devops

我正在尝试使用以下代码获取 TFS 服务器(2017)上所有团队的列表,并且客户端库版本是最新预览版。

var teamclient = await Connection.GetClientAsync<TeamHttpClient>();
var teams = await teamclient.GetAllTeamsAsync();

这会导致以下异常: API 资源位置 7a4d9ee9-3433-4347-b47a-7a80f1cf307e 未注册(出于隐私原因删除了 tfs 链接)

最佳答案

您可以尝试以下代码来列出所有用户:

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("url"));
tfs.EnsureAuthenticated();

IGroupSecurityService gss = tfs.GetService<IGroupSecurityService>();

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);

Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

foreach (Identity user in UserId)
{
    Console.WriteLine(user.AccountName);
    Console.WriteLine(user.DisplayName);
}

或者您可以使用以下代码来获取每个团队中的用户:

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace GetUser
{
    class Program
    {
        static void Main(string[] args)
        {
            String collectionUri = "http://TFS2017:8080/tfs/defaultcollection";
            VssCredentials creds = new VssClientCredentials();
            creds.Storage = new VssClientCredentialStorage();
            VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
            TeamHttpClient thc = connection.GetClient<TeamHttpClient>();
            List<IdentityRef> irs = thc.GetTeamMembersAsync("TeamProject", "TeamProjectTeam").Result;

            foreach (IdentityRef ir in irs)
            {
                Console.WriteLine(ir.DisplayName);
            }
        }
    }
}

更新:

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace GetUser
{
    class Program
    {
        static void Main(string[] args)
        {
                        String collectionUri = "http://TFS2017:8080/tfs/defaultcollection";
        VssCredentials creds = new VssClientCredentials();
        creds.Storage = new VssClientCredentialStorage();
        VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
        TeamHttpClient thc = connection.GetClient<TeamHttpClient>();

        List<WebApiTeam> irs = thc.GetTeamsAsync("AgileProject").Result;

        foreach (WebApiTeam ir in irs)
        {
            Console.WriteLine(ir.Name);
        }

        }
    }
}

关于c# - 尝试通过客户端库获取 TFS 用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152648/

相关文章:

c# - 在 MVVM 中放置全局变量的位置(Caliburn micro)

c# - 使用 FlurlClient 的自定义 HttpClientHandler 不使用 ClientCertificate

c# - 如何在 .NET Core 中使用 HttpClientHandler 和 HttpClientFactory

c# - 如何在 ASP.NET Core 1.1 中对使用 HttpContext 的 MVC Controller 进行单元测试

visual-studio - 尝试将文件与服务器版本进行比较时出现问题

tfs - 在 TFS 2008 中找出一个工作项或一组变更集的所有受影响文件的方法?

c# - 循环 DataTable 或更多数据库调用哪个更有效?

c# - 类库中的 Entity Framework 代码优先

c# - Java 和 C# 处理方法绑定(bind)方式的显着差异背后的解释是什么?

TFS 2012 Scrum 错误,父项作为待办事项