2017-08-04 63 views

回答

0

這是無法通過REST API現在實現了,甚至將用戶添加到團隊。一直有相關的UserVoice的:

添加REST API添加成員團隊

https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/6088139-add-rest-api-for-adding-members-to-a-team

你可以使用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?

+0

謝謝!我需要簡單地這樣做以添加一個管理員: 之後,我得到團隊: string token = IdentityHelper.CreateSecurityToken(_team.Identity); securityNamespace.SetPermissions(token,admin.Descriptor,15,0,true); – Nello360