问题
I want to add user / member (who has already VSTS account) in a particular team (Under a particular project) programmatically. From portal we can always do the same. But, I am looking for some REST service that will do the same for me.
In the below documentation is from Microsoft, we get several api related to the team. But it does not provide anything related to 'how do I add existing VSTS account holder to a team'
https://www.visualstudio.com/en-us/docs/integrate/api/tfs/teams
Thanks in advance.
回答1:
Option 1: REST API (not available for now)
For now, there is no such REST API to add members to a team project. But there has an user voice add rest api for adding members to a team which suggest to add this feature for REST API, you can vote and follow up.
Once the REST API is available, you can use REST API programmatically to add member to a team.
Option 2: API
You can also refer the related code under the user voice in comments part (an example for adding members to default team):
public Task<TeamProject> GetProjectAsync(string projectId)
{
var projectClient = this.Connection.GetClient<ProjectHttpClient>();
return projectClient.GetProject(projectId);
}
public async Task GrantProjectAccessAsync(string projectId, string email)
{
var project = await this.GetProjectAsync(projectId);
var client = this.Connection.GetClient<IdentityHttpClient>();
var identities = await client.ReadIdentitiesAsync(Microsoft.VisualStudio.Services.Identity.IdentitySearchFilter.MailAddress, email);
if (!identities.Any() || identities.Count > 1)
{
throw new InvalidOperationException("User not found or could not get an exact match based on email");
}
var userIdentity = identities.Single();
var groupIdentity = await client.ReadIdentityAsync(project.DefaultTeam.Id);
await client.AddMemberToGroupAsync(
groupIdentity.Descriptor,
userIdentity.Id
);
}
To add members for another team, you can use teamClient.GetTeamsAsync method to get teams for the project, and then get the team id by matching the given team name.
来源:https://stackoverflow.com/questions/46933701/how-to-add-member-in-vsts-team-programmatically