团队基础服务器使用Java SDK获取项目的用户

时间:2016-02-26 17:35:09

标签: java tfs tfs-sdk

我正试图通过SDK for Java版本11.0.0获取属于项目的所有用户但是我被卡住了。 使用该代码,我将检索集合和项目:

TeamFoundationServerEntity teamFoundationServer=configurationServer.getTeamFoundationServerEntity(true);
if (teamFoundationServer != null)
{
        ProjectCollectionEntity[] projectCollections = teamFoundationServer.getProjectCollections();
        for (ProjectCollectionEntity pce : projectCollections) {
            System.out.println("Collection: "+pce.getDisplayName()+"  "+pce.getDescription());
            TeamProjectEntity[] tpe=pce.getTeamProjects();
            for (TeamProjectEntity teamProjectEntity : tpe) {
                System.out.println("    teamProjectEntity: "+teamProjectEntity.getDisplayName()+" * "+teamProjectEntity.getProjectURI());
            }
        }
}

此外,下面的代码取自下载的zip中的示例,并具有组信息:

GUID[] resourceTypes = new GUID[]{
        CatalogResourceTypes.PROJECT_COLLECTION
    };
    CatalogResource[] resources =
        configurationServer.getCatalogService().queryResourcesByType(resourceTypes, CatalogQueryOptions.NONE);

    if (resources != null)
    {
        for (CatalogResource resource : resources)
        {
            String instanceId = resource.getProperties().get("InstanceId");
            TFSTeamProjectCollection tpc = configurationServer.getTeamProjectCollection(new GUID(instanceId));

            System.out.println("TFSTeamProjectCollection");
            System.out.println("\tName: " + tpc.getName().toString());
            System.out.println("\tURI: " + tpc.getBaseURI());

            ProjectCollection pc=tpc.getWorkItemClient().getProjects();

            for (Project project : pc) {
                System.out.println("---"+project.getName()+" * "+project.getID());

                String[] grps=tpc.getWorkItemClient().getGroupDataProvider(project.getName()).getGroups();
            }
       }
   }

我找到了IdentityManagementService类

IdentityManagementService ims=new IdentityManagementService(configurationServer);

但我不知道如何使用 listApplicationGroups readIdentities 方法,这些方法可能对查找解决方案很有用。 有没有人想让每个项目组中的用户都知道?

在@Cece之后再进行一些试用 - MSFT回答并查看blog和书籍Microsoft Team Foundation Server 2015 Cookbook。使用此代码

TeamFoundationIdentity[] appGroups=ims.listApplicationGroups(project.getURI(), ReadIdentityOptions.EXTENDED_PROPERTIES);
for (TeamFoundationIdentity group : appGroups) 
{
    System.out.println(group.getDisplayName());
    TeamFoundationIdentity[] groupMembers= ims.readIdentities(new IdentityDescriptor[]{group.getDescriptor()}, MembershipQuery.EXPANDED, ReadIdentityOptions.EXTENDED_PROPERTIES);
    for (TeamFoundationIdentity member : groupMembers) 
    {
        for(IdentityDescriptor memberID : member.getMembers())
        {
            TeamFoundationIdentity memberInfo=ims.readIdentity(IdentitySearchFactor.IDENTIFIER, memberID.getIdentifier(), MembershipQuery.EXPANDED, ReadIdentityOptions.EXTENDED_PROPERTIES);
            System.out.println(memberInfo.getDisplayName());
        }
    }
}

变量 appGroups 始终为空。也许方法 project.getURI()不合适?如果我把 null

TeamFoundationIdentity[] tfi=ims.listApplicationGroups(null, ReadIdentityOptions.INCLUDE_READ_FROM_SOURCE);
for (TeamFoundationIdentity teamFoundationIdentity : tfi) {
    System.out.println(teamFoundationIdentity.getDisplayName());
    System.out.println(teamFoundationIdentity.getDescriptor().getIdentityType());
    IdentityDescriptor[] mbs=teamFoundationIdentity.getMembers();
    for (IdentityDescriptor mb : mbs) {
        TeamFoundationIdentity mbi=ims.readIdentity(mb, MembershipQuery.EXPANDED, ReadIdentityOptions.EXTENDED_PROPERTIES);
        System.out.println(mbi.getProperties());
    }
}

输出

[DefaultCollection]\Project Collection Administrators
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Build Administrators
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Build Service Accounts
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Proxy Service Accounts
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Service Accounts
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Test Service Accounts
Microsoft.TeamFoundation.Identity
[DefaultCollection]\Project Collection Valid Users
Microsoft.TeamFoundation.Identity

为什么我无法在 listApplicationGroups 方法中使用 project.getURI()获取贡献者,读者和其他群组?我只能用

来获取它们
String[] grps=tpc.getWorkItemClient().getGroupDataProvider(project.getName()).getGroups();

2 个答案:

答案 0 :(得分:0)

检查this博客。在此博客中,作者使用IGroupSecurityService服务获取应用程序组列表,并获取用户所属组的详细信息。

但是现在,IGroupSecurityService已经过时了。您需要改为使用IIdentityManagementServiceISecurityService

代码段应如下所示:

var sec = tfs.GetService<IIdentityManagementService>();
Identity[] appGroups = sec.ListApplicationGroups(Scope Uri);


foreach (Identity group in appGroups)
{
     Identity[] groupMembers = sec.ReadIdentities(SearchFactor.Sid, new string[] { group.Sid }, QueryMembership.Expanded);

     foreach (Identity member in groupMembers)
     {
         var groupM = new GroupMembership {GroupName = member.DisplayName, GroupSid = member.Sid};

         if (member.Members != null)
         {
             foreach (string memberSid in member.Members)
             {
                 Identity memberInfo = sec.ReadIdentity(SearchFactor.Sid, memberSid, QueryMembership.Expanded);

                 var userName = memberInfo.Domain + "\\" + memberInfo.AccountName;

详细步骤,您可以查看博客。

答案 1 :(得分:0)

对于对这两个链接感兴趣的人,有答案: get groups of projectget members of a group