2017-08-30 46 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。這將得到與手動將繼承關閉關閉相同的結果。就像解決方法。 –