Search code examples
javatfstfs-sdk

Team foundation server getting users of a project using Java SDK


i'm trying to get all the users that belong to a project through the SDK for Java version 11.0.0 but i'm stuck. With that code i retrieve the collections and the projects:

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());
            }
        }
}

Also with the following code taken from the example in the downloaded zip and has the groups information:

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();
            }
       }
   }

I have found the class IdentityManagementService

IdentityManagementService ims=new IdentityManagementService(configurationServer);

but i don't know how to use the listApplicationGroups and readIdentities methods that maybe are useful to find a solution. Has anyone some idea to get the users in every project group?

A few more trial after @Cece - MSFT answer and looking at the blog and the book Microsoft Team Foundation Server 2015 Cookbook. Using this code

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());
        }
    }
}

the variable appGroups is always empty. Maybe the method project.getURI() is not suitable? If I put 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());
    }
}

the output is

[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

Why I can't get the Contributors, Readers and the other groups with project.getURI() in listApplicationGroups method? I can only get them with

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

Solution

  • For anyone who is interested in this two link there is the answer: get groups of project and get members of a group