【发布时间】:2017-08-07 21:16:47
【问题描述】:
【问题讨论】:
【问题讨论】:
目前,Rest API 无法实现这一点,即使将用户添加到团队也是如此。有一个相关的用户声音:
添加rest api用于将成员添加到团队
您可以在服务器端使用 TFS API 服务器来实现,示例代码如下:
static void Main(string[] args)
{
// Connect to the TFS server and get the team project URI.
var collection = GetServer("server_uri");
var projectUri = GetProjectUri(collection, "project_name");
// Retrieve the default team.
TfsTeamService teamService = collection.GetService<TfsTeamService>();
TeamFoundationTeam defaultTeam = teamService.GetDefaultTeam(projectUri, null);
// Get security namespace for the project collection.
ISecurityService securityService = collection.GetService<ISecurityService>();
SecurityNamespace securityNamespace = securityService.GetSecurityNamespace(FrameworkSecurity.IdentitiesNamespaceId);
// Use reflection to retrieve a security token for the team.
MethodInfo mi = typeof(IdentityHelper).GetMethod("CreateSecurityToken", BindingFlags.Static | BindingFlags.NonPublic);
string token = mi.Invoke(null, new object[] { defaultTeam.Identity }) as string;
// Retrieve an ACL object for all the team members.
var allMembers = defaultTeam.GetMembers(collection, MembershipQuery.Expanded).Where(m => !m.IsContainer);
AccessControlList acl = securityNamespace.QueryAccessControlList(token, allMembers.Select(m => m.Descriptor), true);
// Retrieve the team administrator SIDs by querying the ACL entries.
var entries = acl.AccessControlEntries;
var admins = entries.Where(e => (e.Allow & 15) == 15).Select(e => e.Descriptor.Identifier);
// Finally, retrieve the actual TeamFoundationIdentity objects from the SIDs.
var adminIdentities = allMembers.Where(m => admins.Contains(m.Descriptor.Identifier));
}
虽然展示了如何查询团队管理员,但添加管理员也并不难。
TeamFoundationIdentity admin
string token = IdentityHelper.CreateSecurityToken(team.Identity);
securityNamespace.SetPermissions(token, admin.Descriptor, 15, 0, true);
确保您使用的帐户具有足够的权限来添加团队管理员来运行代码。更多细节请参考问题中的这个答案:TFS API - How to get a Team's Adminstrator?
【讨论】: