2017-08-17 64 views
1

我正在製作一個控制檯應用程序,它接受用戶輸入以創建一個新區域。基於該名稱,組將生成並放入項目中,並硬編碼到應用程序中。截至目前,下面的代碼將做到這一點:如何使用API​​將項目中的組添加到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

似乎您想要將新創建的組以編程方式添加到新創建的區域安全對話框,然後爲該組授予權限。

不幸的是,現在沒有TFS ADK可供您以編程方式將組添加到區域安全對話框。但是,爲什麼要先將新創建的組添加到該Project Security對話框,然後爲其授予權限?即使在「區域安全性」對話框中未顯示該組,也可以直接授予權限

您可以使用TFSSecurity命令行來爲您工作,或者僅使用TFS API來授予權限。使用IGroupSecurityService interface

此博客文章還向您展示瞭如何使用API​​:TFS SDK Get Groups Users Permissions using TFS API。你也可以看一下這個示例展示瞭如何設置區域的權限:Set Security For Area/Iteration

+0

另外,要爲組設置權限,必須傳遞一個對應於您試圖允許/拒絕的權限的int。但是,在[TeamProjectPermissions類](https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.framework.common.teamprojectpermissions(v = vs.120).aspx)中,它們缺少一個對應的整數創建標記定義_,刪除此項目中的工作項目_和_永久刪除此項目中的工作項目__的權限。所以我不知道如何將這些權限設置到項目中的某個組 –

+0

@JoshEvans對不起,第一個誤解。已更新我的回覆。 –

+0

謝謝帕特里克,很棒的鏈接。但是,您是否注意到我在上述評論中的問題? –

相關問題