Get all users of an Ado or TFS project

86 Views Asked by At

I am unable to find any ADO .net client libraries/rest apis to find all the users related to a particular project, project can be belong to ADO server or TFS (>2013) and ADO service.

I used GraphHttpClient but it is deprecated now and can not be used with TFS, I guess. I am unable to find its documentation either. Also I am facing trouble how to use Microsoft Graph client to do this.

1

There are 1 best solutions below

5
Kevin Lu-MSFT On

To get the users of Azure DevOps Service, we can use the API: Users - List with the variable scopeDescriptor to get the list of user in one project.

GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?scopeDescriptor={scopeDescriptor}&api-version=6.0-preview.1

Refer to the following steps:

Step1: Use Rest API: List all projects and get the project ID.

GET https://dev.azure.com/{organization}/_apis/projects?api-version=6.0

Step2:Get the one project scopeDescriptor via Descriptors - Get

GET https://vssps.dev.azure.com/{organization}/_apis/graph/descriptors/{ProjectID}?api-version=5.0-preview.1

For example:

enter image description here

We can record the value(scp...) of the value field in the response. The value is scopeDescriptor.

Step3: Add the related values to the Rest API:

GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?scopeDescriptor={scopeDescriptor}&api-version=6.0-preview.1

Then we could only get the list of users in one project.

If you need to run the Rest APIs in TFS or Azure DevOps Server, you can change to use the related Rest API version.

For example: You can check it in the dropdown list of the doc.

enter image description here

Update:

To get the users in the TFS or Azure DevOps project, you can use the client API you mentioned above to retrieve the user list.

You can check the group: [projectname]\\Project Valid Users to get all users in the project.

Here is an example:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System.Linq;
using System.IO;

namespace Getuserlist

{

    class Program

    {
        static void Main(string[] args)

        {

            TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("http://server:8080/tfs"));

            IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();

            TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "[projectname]\\Project Valid Users", MembershipQuery.Expanded, ReadIdentityOptions.None);

            TeamFoundationIdentity[] ids = ims.ReadIdentities(tfi.Members, MembershipQuery.None, ReadIdentityOptions.None);

            using (StreamWriter file = new StreamWriter("userlist.txt"))

                foreach (TeamFoundationIdentity id in ids)

                {
                    if (id.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity")

                    { Console.WriteLine(id.DisplayName); }
                    //{ Console.WriteLine(id.UniqueName); }

                    file.WriteLine("[{0}]", id.DisplayName);
                }

            var count = ids.Count(x => ids.Contains(x));
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}