I want to add users programmatically to a Team Project. What I found out to be the solution was this:
IGroupSecurityService gss = (IGroupSecurityService)objTFS.GetService(typeof(IGroupSecurityService));
Identity identity = gss.ReadIdentity(SearchFactor.AccountName, "Group name", QueryMembership.None);
gss.AddMemberToApplicationGroup(groupProject.Sid, member.Sid);
But this only work for groups/users known to TFS.
I want to add a Windows account to TFS
For example:
Windows account name: TestTFS
Password:123456
Then add the TestTFS to TFS programmatically.
I know there is a tool named TeamFoundation Administration Tool can do that, but I do not want to use it.
In TFS2012, the
IGroupSecurityService
was marked obsolete and replaced withIIdentityManagementService
.You can use
IIdentityManagementService.ReadIdentity()
along withIIdentityManagementService.AddMemberToApplicationGroup()
to add Windows users to TFS groups, even if those Windows users are not known to TFS yet.This is accomplished by specifying the
ReadIdentityOptions.IncludeReadFromSource
option.Below is an example of adding a Windows user
VSALM\Barry
to theFabrikam Fiber Web Team
(TFS Group), in theFabrikamFiber
Team Project, in thehttp://vsalm:8080/tfs/FabrikamFiberCollection
server/collection.You will need to add references to:
Microsoft.TeamFoundation.Client
andMicrosoft.TeamFoundation.Common
I had the same issue, finally following code worked
Namespace :
using System.Security.Principal;
In order to perform this operation via the TFS API, you need access to 2 levels of information.
The sid of the user you want to add,
The sid of the group you want to add the user to => The code you have shown in your post will get you the id of the group you want to add the user to.
I found 2 links with sample code i think can be helpful to you,
http://www.koders.com/csharp/fid950BA08A78C801F46B18D196597CBB40E2E2B29D.aspx
Source code of the tfs admin tool: http://tfsadmin.codeplex.com/SourceControl/changeset/view/83047#1591025