Search code examples
c#tfs-sdk

Use VersionControlExt.Explorer outside Visual Studio


I'm developing a TFS tool to assist the developers in our company.

This said tool needs to be able to "browse" the TFS server like in the Source Control Explorer. I believe that by using VersionControlExt.Explorer.SelectedItems, a UI will pop-up that will enable the user to browse the TFS server (please correct me if I'm wrong).

However, VersionControlExt is only accessible when developing inside Visual Studio (aka Plugin). Unfortunately, I am developing a Windows Application that won;t run inside VS.

So the question is, Can I use VersionControlExt outside of Visual Studio? If yes, how?

Here's an attempt on using the Changset Details Dialog outside of Visual Studio

string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Assembly vcControls = Assembly.LoadFile(path + @"\Microsoft.TeamFoundation.VersionControl.Controls.dll");
Assembly vcClient =   Assembly.LoadFile(path + @"\Microsoft.TeamFoundation.VersionControl.Client.dll");

Type dialogChangesetDetailsType = vcControls.GetType("Microsoft.TeamFoundation.VersionControl.Controls.DialogChangesetDetails",true);
Type[] ctorTypes = new Type[3] {vcClient.GetType("Microsoft.TeamFoundation.VersionControl.Client.VersionControlSever"),

vcClient.GetType("Microsoft.TeamFoundation.VersionControl.Client.Changeset"), typeof(System.Boolean)};

ConstructorInfo ctorInfo = dialogChangesetDetailsType.GetConstructor(ctorTypes);
Object[] ctorObjects = new Object[3] {VersionControlHelper.CurrentVersionControlServer, uc.ChangeSet, true};
Object oDialog = ctorInfo.Invoke(ctorObjects);
dialogChangesetDetailsType.InvokeMember("ShowDialog", BindingFlags.InvokeMethod, null, oDialog, null);

Solution

  • Turns out that I don't really need that Explorer.

    I accomplished this by using the TreeView control and VersionControlServer.GetItems().

    Code snippet below:

            treeView.Sort(); //Alphabetically ordered
    
            //Get Initial List of Projects
            try
            {
                ItemSet itemSet = vcs.GetItems(@"$/", RecursionType.OneLevel);
    
                foreach (Item item in itemSet.Items)
                {
                    if (item.ServerItem == @"$/") //Ignore self
                        continue;
    
                    TreeNode node = new TreeNode(item.ServerItem, new TreeNode[] { new TreeNode() });
                    node.Tag = item.ServerItem;
    
                    if (item.DeletionId != 0)
                        node.ForeColor = Color.Red;
    
                    treeView.Nodes.Add(node);
                }
            }
    

    Then, everytime the user expand the nodes, I get all the items under that node.

    TreeNode curNode = e.Node;
                    curNode.FirstNode.Remove(); //Remove blank dummy node
    
    
                    ItemSet items = vcs.GetItems(curNode.Tag.ToString(), VersionSpec.Latest, RecursionType.OneLevel, DeletedState.Any, ItemType.Folder);
    
                    foreach (Item item in items.Items)
                    {
                        if (item.ServerItem == curNode.Tag.ToString()) //Ignore self
                            continue;
    
                        string Name = System.IO.Path.GetFileName(item.ServerItem);
    
                        TreeNode node = new TreeNode(Name, new TreeNode[] { new TreeNode() });
                        node.Tag = item.ServerItem;
    
                        if (item.DeletionId != 0)
                            node.ForeColor = Color.Red;
    
                        curNode.Nodes.Add(node);
                    }