VSTS - 使用VSTS REST API获取团队项目管理员

时间:2017-11-28 12:41:59

标签: rest api tfs azure-devops

是否可以为每个项目获得所有项目管理员?我发现我可以使用此API获取所有项目及其团队成员:https://www.visualstudio.com/en-us/docs/integrate/api/tfs/teams#get-a-teams-members

但后来我得到了团队项目成员的完整列表,而不是他们的权限。我想要一个管理员列表,以便我可以联系他们关于他们的TeamProject。

提前致谢!

BTW,使用TFS 2017

2 个答案:

答案 0 :(得分:0)

目前没有这样的REST API可以从VSTS组(例如项目管理员)获取成员。

但是有一个用户语音REST API for a better Projects and Team Management在建议中包含类似的功能,您可以投票和跟进。

答案 1 :(得分:0)

无法通过Rest API实现,但可以使用SOAP API实现。以下是供您参考的代码示例:

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

namespace GetAdmin
{
    class Program
    {
        static void Main(string[] args)
        {
            string projectname = "projectname";
            string groupname = $"[{projectname}]\\Project Administrators";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri("https://vstsaccount.visualstudio.com/"));
            IIdentityManagementService idms = ttpc.GetService<IIdentityManagementService>();
            TeamFoundationIdentity admingroup = idms.ReadIdentity(IdentitySearchFactor.AccountName,groupname,MembershipQuery.Direct,ReadIdentityOptions.IncludeReadFromSource);
            foreach (IdentityDescriptor tfi in admingroup.Members)
            {
                TeamFoundationIdentity member = idms.ReadIdentity(tfi,MembershipQuery.Expanded, ReadIdentityOptions.ExtendedProperties);
                Console.WriteLine(member.DisplayName);
                Console.WriteLine(member.GetProperty("Mail"));
            }
            Console.ReadLine();
        }
    }
}

Powershell脚本:

$dllpath1 = "D:\\net45\\Microsoft.TeamFoundation.Client.dll";
$dllpath2 = "D:\\net45\\Microsoft.TeamFoundation.Common.dll";
$dllpath3 = "D:\\net45\\Microsoft.VisualStudio.Services.Common.dll";
$dllpath4 = "D:\\net45\\Microsoft.VisualStudio.Services.Client.Interactive.dll";

[System.Reflection.Assembly]::LoadFrom($dllpath1);
[System.Reflection.Assembly]::LoadFrom($dllpath2);
[System.Reflection.Assembly]::LoadFrom($dllpath3);
[System.Reflection.Assembly]::LoadFrom($dllpath4);

    $uri = "https://xxx.visualstudio.com/";
    $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri)
    $idservice = $tfs.GetService("Microsoft.TeamFoundation.FrameWork.Client.IIdentityManagementService")
    $projectname = "xxx"
    $groupname = "[" + $projectname + "]\Project Administrators";
    $admingroup = $idservice.ReadIdentity([Microsoft.TeamFoundation.FrameWork.Common.IdentitySearchFactor]::AccountName,$groupname,[Microsoft.TeamFoundation.FrameWork.Common.MembershipQuery]::Direct,[Microsoft.TeamFoundation.FrameWork.Common.ReadIdentityOptions]::IncludeReadFromSource)
    foreach ($id in $admingroup.Members)
    {
        $member = $idservice.ReadIdentity($id,[Microsoft.TeamFoundation.FrameWork.Common.MembershipQuery]::Expanded, [Microsoft.TeamFoundation.FrameWork.Common.ReadIdentityOptions]::ExtendedProperties)
        Write-Host $member.DisplayName
        Write-Host $member.GetProperty("Mail")
    }