1

I want to add a Team's Administrator by using TFS API REST or implementing a method in c#...can you help me? Is there a way to do it by code, instead of doing it from tfs web site as you can see in image? thank's!

enter image description here

polinello
  • 27
  • 1
  • 6

1 Answers1

0

This is couldn't achieved by Rest API for now, even adding a user to team. There has been a related uservoice:

Add rest api for adding members to a team

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

You could use TFS API server on the server side to achieve this, sample code as below:

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));       
}

Even though it's show how to query team admin, it's also not hard to add a admin.

TeamFoundationIdentity admin

 string token = IdentityHelper.CreateSecurityToken(team.Identity); 

 securityNamespace.SetPermissions(token, admin.Descriptor, 15, 0, true);

Make sure you are using the account with enough permission to add team admin to run the code. More details please refer this answer in the question: TFS API - How to get a Team's Adminstrator?

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • thank you! I needed simply to do this to add an admin: after that I get the team: string token = IdentityHelper.CreateSecurityToken(_team.Identity); securityNamespace.SetPermissions(token, admin.Descriptor, 15, 0, true); – polinello Aug 07 '17 at 15:36