Get members of TFS group

Dilshod picture Dilshod · May 28, 2013 · Viewed 7.8k times · Source

I created a TFS group for our company programmers and I am trying to get list of programmers on that group. This here is so far I tried.

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

The list is empty.

Am I missing something?

Answer

JF Beaulieu picture JF Beaulieu · Aug 9, 2013

This will return a list of Microsoft.TeamFoundation.Server.Identity objects that are the actual TFS users you are looking for. You can then serialize these objects to your own entities so you can do whatever you want with them afterwards.

Here is how it's done:

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