0

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

2 Answers2

0

Check this blog. In this blog, author used IGroupSecurityService service to get the list of application groups and get the details of which group the user is a member.

But now, IGroupSecurityService is obsolete. You need to use the IIdentityManagementService or ISecurityService instead.

The code snippet should look like:

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;

Detailed steps, you can check the blog.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Thanks, I have edited the questions providing more details. – Alessio De Rossi Feb 29 '16 at 14:54
  • Check IIdentityManagementService.ListApplicationGroups Method at https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.framework.client.iidentitymanagementservice.listapplicationgroups(v=vs.120).aspx, you need to use Scope Uri. – Cece Dong - MSFT Mar 04 '16 at 02:39
  • where I can find the project Scope URI? – Alessio De Rossi Mar 07 '16 at 10:24
  • Scope is a project URI or URL, including its GUID and connection string.The format is vstfs:///Classification/TeamProject/ GUID. – Cece Dong - MSFT Mar 08 '16 at 02:37
  • I've printed _project.getURI()_ and _project.getGUID()_ and | obtain right what you've written. For example project Uri: vstfs:///Classification/TeamProject/d5d1c7cc-63a1-4cf7-8491-0e237e92d291 and GUID: d5d1c7cc-63a1-4cf7-8491-0e237e92d291. Then why _listApplication_ method does not return something? – Alessio De Rossi Mar 08 '16 at 13:34
0

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