【问题标题】:How to get Unique Display name for VSTS/TFS identity?如何获取 VSTS/TFS 身份的唯一显示名称?
【发布时间】:2018-10-01 13:57:17
【问题描述】:

我正在使用 VSTS/TFS Rest .Net libraries 编写一个 .Net 应用程序,在一个地方我需要更新工作项的 System.AssignedTo 字段值,虽然我确实想遵守 new(ish), unique displayname rules 的身份工作项字段,但我很难找到一种方法来获取给定身份的唯一显示名称。

旧的 / 客户端对象模型 does have an explicit helper method 来获取这些唯一名称,但我没有找到任何可以提供相同名称的 rest 端点或客户端 api 方法。

所以我想知道,给定一个身份列表,我如何获得它们对应的唯一显示名称,我可以使用它来明确设置身份工作项字段?

【问题讨论】:

    标签: c# .net rest tfs azure-devops


    【解决方案1】:

    您可以尝试以下代码来获取唯一名称:

    using System;
    
    using System.Collections.Generic;
    
    using Microsoft.TeamFoundation.Client;
    
    using Microsoft.TeamFoundation.Framework.Client;
    
    using Microsoft.TeamFoundation.Framework.Common;
    
    namespace ConsoleApplication3
    
    {
    
        class Program
    
        {
            static void Main(string[] args)
    
            {
    
                TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("http://tfsserver:8080/tfs"));
    
                IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();
    
                TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "[TEAM FOUNDATION]\\Team Foundation Valid Users", MembershipQuery.Expanded, ReadIdentityOptions.None);
    
                TeamFoundationIdentity[] ids = ims.ReadIdentities(tfi.Members, MembershipQuery.None, ReadIdentityOptions.None);
    
                foreach (TeamFoundationIdentity id in ids)
    
                {
                    if (id.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity")
    
                    {
                        Console.WriteLine(id.DisplayName);
                        Console.WriteLine(id.UniqueName);
                    }
    
                }
    
                Console.ReadLine();
    
            }
        }
    }
    

    【讨论】:

    • 晚上好,谢谢 - 但该代码示例显然使用的是旧的客户端对象模型。如上所述,我只使用新的基于 REST 的 .Net 客户端库,并且想知道如何使用这些库。
    • @JörgB。您使用 VSTS 还是本地 TFS?如果您使用 VSTS,则有 AccountsProfiles REST api 可用于获取配置文件。如果您使用本地 TFS,请检查以下 rest api http://server:8080/tfs/_api/_common/GetUserProfile?__v=5
    【解决方案2】:
                String collectionUri = "http://collectionurl/";
                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("ProjectName","TeamName").Result;
                foreach (IdentityRef ir in irs)
                {
                    Console.WriteLine(ir.UniqueName);
                    Console.WriteLine(ir.DisplayName);
                }
    

    【讨论】:

    • 谢谢埃迪!我曾经使用过它,但没有发现迭代所有团队以在给定团队项目中获得全部或一个特定身份的效率并不高。但从技术上讲,它是有效的,所以它已经足够好了。干杯!
    【解决方案3】:
    foreach (var workItem in workItems)
    {
         if (workItem.Fields.ContainsKey("System.AssignedTo"))
         {
             var person = (IdentityRef)workItem.Fields["System.AssignedTo"];
             string codereview_reviewer = person.DisplayName;
              Console.WriteLine(codereview_reviewer);
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      相关资源
      最近更新 更多