DCSIMG
TFS API Part 4: Get TFS User List (Mail, Sid, Account, Domain) - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

TFS API Part 4: Get TFS User List (Mail, Sid, Account, Domain)

TFS API Part 4: Get TFS User List (Mail, Sid, Account, Domain)

In my previous posts TFS API Part 2: Domain Picker Using Registered Servers (Cache) I demonstrate how to connect TFS and how to get TFS Projects.

In this post I’ll show how to get User List from TFS by groups.

First add reference for Microsoft.TeamFoundation, Microsoft.TeamFoundation.Client, Microsoft.TeamFoundation.Common.dll
located in - C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\

Download Demo Project

Add using for Proxy,Client and Server.

*** Good to know -> An Identity structure represents a user, group, or Team Foundation Server application group, along with some of their attributes

using Microsoft.TeamFoundation.Proxy;

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.Server;

Connect to TFS and call GetTfsUsers(dp.SelectedServer) with the TFS.

Create IGroupSecurityService from TFS.

IGroupSecurityService gss = (IGroupSecurityService)server.GetService(typeof(IGroupSecurityService));

 

# Read Identitys

Using gss you can read all Identitys in TFS.

SearchFactor

AccountName -Logon name (for Windows identities) or an application group name, possibly qualified by domain name, such as AdventureWorks\JAaberg, ProjectURI\groupname, or GUID\groupname.

AdministrativeApplicationGroup - An administrative application group.

DistinguishedName - A LDAP escaped distinguished name, optionally prepended by LDAP://.

EveryoneApplicationGroup - The Team Foundation Server Everyone application group.

ServiceApplicationGroup - The service application group.

Sid - Contains a SID 

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);

 

image

 

Identity SIDS = gss.ReadIdentity(SearchFactor.ServiceApplicationGroup, "Team Foundation Valid Users", QueryMembership.Expanded);

 

image

QueryMembership

Direct- Each Identity includes its direct members. However, these member identities will contain null members.

Expanded - Each returned identity includes its direct members. Each of these member identities contain their direct member identities, and so on.

None - No information about membership is returned, and corresponding properties are set to null. 

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);

image

 

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Direct);

image

#

# Getting User List

To get Identity for each user use ReadIdentities by Sid.

Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

 

image

 

Download Demo Project

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 14, 2009 2:04 PM

Shai Raiten said:

TFS API Part 7: Use IEventService To Get User Event Subscriptions Over the previous posts I talked about

# January 21, 2009 12:12 AM

Lammy said:

Many thanks Shai. Saved me many, many hours of work. To find this was a gift...Toda raba.

# April 10, 2009 1:22 AM

Bubma said:

Hello Shai,

thanks for the article,

I was just wondering,  I have a custom web app for the tfs and on the login I am asking the user to choose the project that he wants to log in to.

I somehow cannot list only those projects that the user has access to, I now list all of them, and it is a security issue.

I tried with the IGroupSecurityService but i get an error when the user trying to access the service is not in the administrator group.....

Is there any workaround to it?

Thank you,

Cheers

# June 5, 2009 12:45 PM

Stainless steel pipe said:

Thanks for the great article here at your blog. I know it will help me and many others.

# February 5, 2010 11:15 AM

santosh said:

Is there any code sample how to achive the same in Dev10 .

# May 28, 2010 5:00 PM

Shai Raiten said:

TFS API Part 25: Get TFS User List (Mail Sid, Account, Domain) – TFS 2010 One of my first posts on TFS

# June 16, 2010 7:49 PM

Kelly said:

I'm indebted to Shai.  Shai is one of my best friends today. :)

# August 18, 2010 4:50 AM

Kelly said:

I had to change a couple lines of code to get past some deprecated properties.  Changed these lines:

From:

GetTfsUsers(dp.SelectedServer);

To:

GetTfsUsers(dp.SelectedTeamProjectCollection);

From:

private void GetTfsUsers(TeamFoundationServer server)

To:

private void GetTfsUsers(TfsTeamProjectCollection tpk); //and updated all server refs to tpk

From:

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);

To:

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);

# August 18, 2010 4:57 AM

Riddhi said:

TfsConfigurationServer tfai = new TfsConfigurationServer(tfsUri, (ICredentials)credential);

IGroupSecurityService gss = tfai.GetService<IGroupSecurityService>();

           Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);

           Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

I am getting gss as null. Does this requires extra setting ?

# June 15, 2011 5:47 PM

shair said:

Hi,

Did you tried to download my example? you code is different. Also if you are working against 2010 - blogs.microsoft.co.il/.../tfs-api-part-25-get-tfs-user-list-mail-sid-account-domain-tfs-2010.aspx

# June 18, 2011 5:52 PM

Chris Nash said:

// Connect to the team project collection.

TfsConfigurationServer tfsServer = m_tfsServer;

Guid collectionGuid = m_collectionGuid;

TfsTeamProjectCollection tpc = tfsServer.GetTeamProjectCollection(collectionGuid);

// Get the group security service.

var gss = tpc.GetService<IGroupSecurityService2>();

// Retrieve each user's SID.

Identity sids = gss.ReadIdentity(SearchFactor.AccountName, "[My Team Project]\\Contributors", QueryMembership.Expanded);

// Resolve to named identities.

Identity[] users = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.None);

# June 22, 2011 7:26 PM

Symons said:

Hi,shair!

I am a newer!

I did not understand the factorValue? So what is the "Team Foundation Valid Users"?

Thank you

Cheers

# December 1, 2011 5:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: