如何使用API​​在TFS中将项目组添加到区域?

时间:2017-08-17 16:15:14

标签: c# visual-studio tfs

我正在创建一个控制台应用程序,它接受用户输入来创建一个新区域。基于该名称,生成组并将其放入项目中,硬编码到应用程序中。截至目前,以下代码将执行此操作:

Project that all areas will be placed into
    -> Areas
        -> New area just created
    -> Security
        -> Groups
            -> Newly created groups

但是,我也希望将这些群体放入该地区:

Project that all areas will be placed into
    -> Areas
        -> New area just created
            -> Security
                -> Groups
                    -> Newly created groups
    -> Security
        -> Groups
            -> Newly created groups

现在,当您查看该区域的安全时,其中只有默认组,而不是我创建的组。

我无法通过API找到管理区域安全性的任何内容。

public class AreaBuilder
    {
        static IIdentityManagementService _ims;
        static List<TeamFoundationIdentity> m_groups = new List<TeamFoundationIdentity>();
        static void Main(string[] args)
        {
            string projectName = "some_project";
            string mainUri = "http://something:8080/tfs";
            string collectionUri = "http://something:8080/tfs/some_collection";

            Console.WriteLine("Enter the name of an area that you'd like to create: \n\n");
            Console.Write("AreaBuilder> ");
            string areaName = Console.ReadLine();

            // Get the structure of the groups and their privileges
            AreaStructure structure = new AreaStructure(areaName);
            Dictionary<string, List<AreaStructure.Privileges>> groups = structure.getGroups();

            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri));
            VersionControlServer vcs = tpc.GetService<VersionControlServer>();

            try
            {
                Microsoft.TeamFoundation.VersionControl.Client.TeamProject project = vcs.GetTeamProject(projectName);
                IGroupSecurityService gss = tpc.GetService<IGroupSecurityService>();

                foreach(string groupName in groups.Keys)
                {
                    gss.CreateApplicationGroup(project.ArtifactUri.AbsoluteUri, groupName, null);
                    Console.WriteLine(groupName + " created.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not create group: " + e.ToString());
            }

            WorkItemClassificationNode node = new WorkItemClassificationNode()
            {
                Name = areaName,
                StructureType = TreeNodeStructureType.Area,
                Children = new List<WorkItemClassificationNode>()
            };

            // Get the connection
            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

            // Get the work item client
            WorkItemTrackingHttpClient workItemTrackingClient = connection.
                GetClient<WorkItemTrackingHttpClient>();

            // Create the new area
            WorkItemClassificationNode area = workItemTrackingClient.CreateOrUpdateClassificationNodeAsync(
                node,
                projectName,
                TreeStructureGroup.Areas).Result;

            // Get the project client
            ProjectHttpClient projectClient = connection.GetClient<ProjectHttpClient>();

            // Get the projects in the project client
            IEnumerable<TeamProjectReference> projects = projectClient.GetProjects().Result;

            Console.ReadLine();
        }
    }

1 个答案:

答案 0 :(得分:1)

似乎您想以编程方式将新创建的组添加到新创建的“区域安全性”对话框中,然后为该组授予权限。

遗憾的是,现在没有可用于以编程方式将组添加到“区域安全性”对话框的TFS ADK。但是,为什么要先将新创建的组添加到该Project Security对话框,然后为其授予权限?即使该组未显示在“区域安全性”对话框中,您也可以直接授予权限

您可以使用TFSSecurity命令行为您完成工作,或者只使用TFS API授予权限。使用IGroupSecurityService interface

此博客文章还向您展示了如何使用API​​:TFS SDK Get Groups Users Permissions using TFS API。您还可以查看此示例,了解如何为区域设置权限:Set Security For Area/Iteration