Search code examples
c#visual-studiotfsvisual-studio-addins

Get TFS Connection in Custom Build Workflow Parameter Editor


So, I am trying to display all available TFS test suites in a custom build workflow parameter editor. See my previous question.

Now I can establish a connection to my TFS instance by using the .Net TFS API just as a normal client Application would. But I would have to embedd the URL to my TFS in the custom assembly, and that's something I would like to avoid.

That got me thinking: This code runs within Visual Studio, so it must somehow be possible to obtain information about the current TFS connection. After searching the web, a lot of different sites showed code about how to do this in a normal Visual Studio extension. So I put together something like this:

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if (provider != null)
        {
            EnvDTE80.DTE2 dte;
            dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");
            MessageBox.Show("Got dte: " + dte.ActiveDocument.ToString());

            TeamFoundationServerExt ext = dte.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt;
            MessageBox.Show("Got tfs: " + ext);

I am able to get the DTE object, calling its ToString() method gives me System.__ComObject, so this part appearantly works. But when I try to get the TeamFoundationServerExt object, I always get null

Any tips why this does not work?


Solution

  • So, as it turns out, you do not have to use the DTE stuff at all.

    You actually can get the TFS connection like this:

    var builddef = (IBuildDefinition)provider.GetService(typeof(IBuildDefinition));
    var tpc = builddef.BuildServer.TeamProjectCollection;
    var tp = builddef.TeamProject;