TFS SDK 10 ——分组(Group)和成员(Member)服务器
这篇来介绍怎样读取TFS服务器上的用户信息 ui
首先TFS默认有以下分组(Group):spa
SharePoint Web Application Servicescode
Team Foundation Administratorsserver
Team Foundation Proxy Service Accountsblog
Team Foundation Service Accountsip
Team Foundation Valid Usersstring
Work Item Only View Users it
其中io
Team Foundation Valid Users 包含其余全部分组
Team Foundation Administrators 包含 Team Foundation Service Accounts
而后每个Collection也有相似如上的默认分组,及一样的包含关系
Project Collection Administrators
Project Collection Build Administrators
Project Collection Build Service Accounts
Project Collection Proxy Service Accounts
Project Collection Service Accounts
Project Collection Test Service Accounts
Project Collection Valid Users
其中
Project Collection Valid Users 包含其余全部分组
Project Collection Administrators包含Project Collection Service Accounts
咱们能够在TFS server 端看到这些。
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
//链接TFS string tpcURL = "http://127.0.0.1:8080/tfs/defaultcollection"; TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tpcURL)); IGroupSecurityService gss = (IGroupSecurityService)tpc.GetService(typeof(IGroupSecurityService));//全部关于分组和成员的相关的操做都是基于IGroupSecurityService的。 //1:获取所有用户 //先查出用户的Id。参数 QueryMembership 指定是否包含其下从属分组的用户 Identity sids = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);
//经过id获取用户的信息,包括名称,邮箱等等 var members = gss.ReadIdentities(SearchFactor.Sid, sids.Members, QueryMembership.Expanded);
//2:获取指定项目下的全部分组 VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer; TeamProject[] allProjects = version.GetAllTeamProjects(true); string projectUrl = allProjects[0].ArtifactUri.AbsoluteUri; Identity[] groups = gss.ListApplicationGroups(projectUrl); //3:添加分组 string groupName = "MyGroup"; string desp = "My Group Description"; string groupSid = gss.CreateApplicationGroup(projectUrl, groupName, desp); //4: 删除分组 gss.DeleteApplicationGroup(groupSid); //5:获取指定分组下的成员 Identity group = groups[0]; var gsids = gss.ReadIdentity(SearchFactor.Sid, group.Sid, QueryMembership.Expanded); Identity[] gmembers = gss.ReadIdentities(SearchFactor.Sid, gsids.Members, QueryMembership.Expanded); //成员不必定是指用户(User) ,也多是分组(Group) //成员的类型有以下几种: //gmembers[0].Type== IdentityType.ApplicationGroup; //gmembers[0].Type== IdentityType.InvalidIdentity //gmembers[0].Type== IdentityType.UnknownIdentityType //gmembers[0].Type== IdentityType.WindowsGroup //gmembers[0].Type== IdentityType.WindowsUser
//6:把指定成员添加到指定分组 string memberSid = gmembers[0].Sid; gss.AddMemberToApplicationGroup(group.Sid, memberSid); //7:把指定成员从指定分组移除 gss.RemoveMemberFromApplicationGroup(group.Sid, memberSid);