1

I was looking to add members to Project using REST API.

I was able to create project using API:

POST https://{instance}/{collection}/_apis/projects?api-version=5.0

Also, I was able to create a team in a project using REST API:

POST https://{instance}/{collection}/_apis/projects/{projectId}/teams?api-version=5.0

However, I was not able to get a REST API to add members to team and project.

Can you please help?

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Uday
  • 151
  • 1
  • 3
  • 15

2 Answers2

3

Devops server 2019 - Is there an REST api to add members to project and team

For this issue,I think there is no out of box rest api to achieve it . The Members - Add rest api is currently not available for Azure DevOps Server 2019.

As a workaround ,we can track this rest api by press F12 in browser then select Network.

Sample request url :

https://collectionName/DefaultCollection/projectId/_api/_identity/AddIdentities?__v=5

Sample request body:

{
    "newUsersJson": "[]",
    "existingUsersJson": "[\"55b98726-c6f5-48d2-976b-xxxxxx\"]",
    "groupsToJoinJson": "[\"7283653f-54b2-4ebf-86c3-xxxxxxx\"]",
    "aadGroupsJson": "[]"
}

In this step ,you need to convert the name of the member you want to add and the name of the team to json, then add to the request body. Here is a case states how to convert a string to JSON in C#.

enter image description here

From this record ,we can get the request url and request body.

I test this with postman and can successfully add a member to a project team. enter image description here

Here are two cases(case1 , case2) with similar problems. You can also refer to them.

The use voice instance in the above case is no longer available. You could submit a new one to our main forum for product suggestions. Our PM and Product Group are reviewing these suggestion regularly and considering take it as plan.

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • apologies for the delay. after couple of trail and error, implement this solution. i can now add members which are already present on tfs and also members on the domain. Thanks for the help. – Uday Dec 03 '19 at 14:49
1

The REST API to add members to projects and team is not documented. As Hugh mentioned we can track the REST API with develop tools (press F12 in browser), however as we can see we can only use the user and team/group GUID in the request json body.

Post https://wsicads2019/DefaultCollection/{project}/_api/_identity/AddIdentities?api-version=5.0

Request Body:

{
    "newUsersJson": "[]",
    "existingUsersJson": "[\"55b98726-c6f5-48d2-976b-xxxxxx\"]",
    "groupsToJoinJson": "[\"7283653f-54b2-4ebf-86c3-xxxxxxx\"]",
    "aadGroupsJson": "[]"
}

For the specific team/groups we can use the REST APIs Projects and teams to get their GUID. For the user, actually it's used the TeamFoundationId, the unique TeamFoundationId is automatically generated when a user is added to Azure DevOps Server. We cannot generate the ID with external tools.

So, to use that REST API, we need to get the TeamFoundationId of the specific user which you want to add it to the projects/teams.

Currently, no REST API to list TeamFoundationId of the users in Azure DevOps Server 2019, however we can get it with Client API:

Below sample for your reference to get the TeamFoundationId of a specific user: (It will also export the user list with their TeamFoundationId to userlist.txt)

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using System.Linq;
using System.IO;

namespace Getuserlist

{

    class Program

    {
        static void Main(string[] args)

        {

            TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("https://wsicads2019"));

            IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();

            TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "[DefaultCollection]\\Project Collection Valid Users", MembershipQuery.Expanded, ReadIdentityOptions.None);

            TeamFoundationIdentity[] ids = ims.ReadIdentities(tfi.Members, MembershipQuery.None, ReadIdentityOptions.None);

            using (StreamWriter file = new StreamWriter("userlist.txt"))

                foreach (TeamFoundationIdentity id in ids)

                {
                    if (id.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity" && id.UniqueName == "Domain\\User")

                    { Console.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId); }

                    file.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId);
                }

            var count = ids.Count(x => ids.Contains(x));
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}
Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55