2013-05-28 36 views
5

的成员,我创建了我们公司的程序员TFS组,我试图让该组程序员的名单。这到目前为止我尝试过。获取TFS组

ICommonStructureService iss = (ICommonStructureService)tfsServer.GetService(typeof(ICommonStructureService)); 
    IGroupSecurityService gss = tfsServer.GetService<IGroupSecurityService>(); 

    Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded); 
    Identity[] _userIds = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None); 

    var companyProgrammers = _userIds.Where(u=>u.MemeberOf.Contains("CompanyProgrammers")).ToList(); 

该列表为空。

我错过了什么吗?

回答

12

这将返回Microsoft.TeamFoundation.Server.Identity对象是您正在寻找的实际TFS用户的列表。然后,您可以将这些对象序列化到您自己的实体中,以便随后您可以随意执行所需的任何操作。

这里是它是如何做:

private List<Identity> ListContributors() 
{ 
    const string projectName = "<<TFS PROJECT NAME>>"; 
    const string groupName = "Contributors"; 
    const string projectUri = "<<TFS PROJECT COLLECTION>>"; 

    TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(projectUri)); 
    ICommonStructureService css = (ICommonStructureService) projectCollection.GetService(typeof(ICommonStructureService)); 
    IGroupSecurityService gss = projectCollection.GetService<IGroupSecurityService>(); 

    // get the tfs project 
    var projectList = css.ListAllProjects(); 
    var project = projectList.FirstOrDefault(o => o.Name.Contains(projectName)); 

    // project doesn't exist 
    if (project == null) return null; 

    // get the tfs group 
    var groupList = gss.ListApplicationGroups(project.Uri); 
    var group = groupList.FirstOrDefault(o => o.AccountName.Contains(groupName)); // you can also use DisplayName 

    // group doesn't exist 
    if (group == null) return null; 

    Identity sids = gss.ReadIdentity(SearchFactor.Sid, group.Sid, QueryMembership.Expanded); 

    // there are no users 
    if (sids.Members.Length == 0) return null; 

    // convert to a list 
    List<Identity> contributors = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.Expanded).ToList(); 

    return contributors; 
} 
+0

的IGroupSecurityService是过时的今天。你将如何使用新的API来做到这一点? –

+2

已过时的代码。根据建议,应该使用IIdentityManagementService或ISecurityService。有谁知道如何使用这些接口? – jwrightmail