2017-08-30 49 views
1

创建一个控制台应用程序,用于TFS中的区域生产自动化,因为我们的团队可以生产更多的项目。一旦应用程序创建区域,我们仍然必须手动执行的一件事是将该区域的继承切换为关闭,因为它是默认情况下是。有没有什么办法可以在C#中使用Visual Studio中的TFS'API?如何使用API​​在TFS中切换区域的继承?

这是我如何创建区域:

static void CreateArea() 
{ 
    string collectionUri = "collectionUri"; 
    string projectName = "projectName"; 
    string areaName = "areaName"; 
    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri));   
    ICommonStructureService commonStructures = tpc.GetService<ICommonStructureService>(); 

    // Root area of the project 
    NodeInfo rootAreaNode = commonStructures.GetNodeFromPath(projectName + "\\Area"); 

    // Create the new area node 
    string newAreaUri = commonStructures.CreateNode(areaName, rootAreaNode.Uri); 

    Console.WriteLine("Created Area: '" + areaName + "'."); 
} 
+0

什么的''继承你指向?您是否指新创建地区的**安全**相关的继承许可? –

回答

1

如果你正在谈论的继承,并希望在更改默认关闭

enter image description here

你可以使用tf权限命令来撤消权限。示例代码如下:

static int Main(string[] args) 
{ 
    if (args.Length == 0 || args.Any(a => !a.StartsWith("$/"))) 
    { 
     Console.WriteLine("Removes all explicit permissions and enables inheritance for a subtree.\n" 
         + "Example: " + Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) + " $/project/path1 $/project/path2"); 
     return 3; 
    } 

    WorkspaceInfo wi = Workstation.Current.GetLocalWorkspaceInfo(Environment.CurrentDirectory); 
    if (wi == null) 
    { 
     Console.WriteLine("Can't determine workspace for current directory: " + Environment.CurrentDirectory); 
     return 2; 
    } 

    var Tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(wi.ServerUri); 
    VersionControlServer VersionControlServer = Tfs.GetService<VersionControlServer>(); 

    Console.WriteLine("Server: {0} Getting permissions...", wi.ServerUri); 
    ItemSecurity[] perms = VersionControlServer.GetPermissions(args, RecursionType.Full); 

    Console.WriteLine("Will remove explicit permissions from the following items:"); 

    var changes = new List<SecurityChange>(); 
    foreach (ItemSecurity perm in perms) 
    { 
     Console.WriteLine(" " + perm.ServerItem); 

     changes.Add(new InheritanceChange(perm.ServerItem, inherit: true)); 
     foreach (AccessEntry e in perm.Entries) 
     { 
      changes.Add(new PermissionChange(perm.ServerItem, e.IdentityName, null, null, PermissionChange.AllItemPermissions)); 
     } 
    } 

    Console.WriteLine("Enter to confirm:"); 
    Console.ReadLine(); 

    var successfulchanges = VersionControlServer.SetPermissions(changes.ToArray()); 
    if (successfulchanges.Length == changes.Count) 
    { 
     Console.WriteLine("Explicit permissions removed from all items"); 
     return 0; 
    } 
    else 
    { 
     Console.WriteLine("Explicit permissions removed only from:"); 
     foreach (var c in successfulchanges) 
     { 
      Console.WriteLine(" " + c.Item); 
     } 

     return 1; 
    } 
} 

更多详细信息,请参阅这个问题:Clearing special permissions from folders in a branch

+0

这将删除所有继承权限,但继承下拉菜单仍有_On_选项。这跟我们能做的一样好,对吗? –

+0

@JoshEvans是的,没有办法直接更改Web门户的UI。这将得到与手动将继承关闭关闭相同的结果。就像解决方法。 –