Saturday 23 September 2017

Extending Team Explorer in Visual Studio 2017

Visual studio extensibility has always been a great feature in Visual Studio and enhance the entire development experience. With Visual Studio 2017, there were a bunch of very substantial changes made with respect to extensibility. Most of these changes comes from the fact that Visual Studio now supports a lighter installation version with bare minimum feature installation as default. There is also the option to have multiple installation on the same machine. So, what does it mean for for extensions? 

VS2017 extensions now following the vsix v3 file format. If you have an extension for earlier visual studio versions and you want to port it to VS2017, it means a whole bunch of changes. Here, I am going to write an extension that demonstrate extending Team Explorer. We will create a very simple extension that has a button on Team Explorer, which will open notepad.

Project Creation & Dependencies

Let's start with creating a new extensibility vsix project. You will only see the option if you had selected the VS SDK option while installing visual studio. Let's call our project TeamExplorerExtSample. Visual Studio 2017 uses .Net Framework 4.6.1, so we select this version.



Once the project is created, you will see a couple of web files and a file called source.extension.vsixmanifest, which contains extension information. We will come to this file later.

Now let's add references to the assemblies we would need to extend Team Explorer. Note that with visual studio 2017, assemblies are not added to GAC so we would need to make sure that all desired assemblies are included in the vsix. To display a navigation button in team explorer, we would need to implement the interface ITeamExplorerNavigationItem2, so we would need to add references to the following assemblies
  •     Microsoft.TeamFoundation.Controls
  •     System
  •     System.ComponentModel.Composition
  •     System.Drawing

VSIX Manifest file:

The manifest file contains information about the extension, it's dependencies, assets and pre-requisites. Double click on the source.extension.vsixmanifest to see details. To extend Team Explorer, the key thing to remember is to add the assembly containing classes that implement Team Explorer interfaces as a MEF component. This will ensure that visual studio loads it up when loading team explorer.

Our VSIX manifest file looks like this


Extending ITeamNavigationItem2

Our extension will create a button in Team Explorer that opens up the notepad application. To do this, we need to extend the ITeamNavigationItem2 interface. The interface is found in Microsoft.TeamFoundation.Control assembly that we have already referenced. We will also need to add TeamExplorerNavigationItem attribute. Our very simple class looks as below.


namespace TeamExplorerExtSample
{
       using System;
       using System.ComponentModel;
       using System.Diagnostics;
       using System.Drawing;
       using Microsoft.TeamFoundation.Controls;
       [TeamExplorerNavigationItem("C9B2CF74-0C87-4CEA-ACA9-8CC1C816D7F3", 1800)]
       public class NotepadNavigationItem : ITeamExplorerNavigationItem2
       {
              public bool IsEnabled => true;
              public int ArgbColor => 0;
              public object Icon => null;
              public string Text => "Open Notepad";
              public Image Image => null;
              public bool IsVisible => true;
              public event PropertyChangedEventHandler PropertyChanged;
              public void Dispose()
              {
                     this.Dispose(true);
                     GC.SuppressFinalize(this);
              }
              protected virtual void Dispose(bool disposing)
              {
              }
              public void Execute()
              {
                     Process.Start("notepad.exe");
              }
              public void Invalidate()
              {
              }
       }
}

As you can see, the only matter we have got in the class is a call to Process.Start to start up notepad. The navigation item appears as below


Click on the button and a new instance of notepad opens up.

Conclusion:

Admittedly. this is a very simplistic extension but contains all the steps you need to extend Team Explorer. You can add classes to add Pages, Sections and Links in Team Explorer, add icons \ images and menu items. The code sample from post is here

10 comments:

Fred said...

Hi Hamid, I noticed that you're using the following attribute for your class:

[TeamExplorerNavigationItem("C9B2CF74-0C87-4CEA-ACA9-8CC1C816D7F3", 1800)]

I can't find any documentation on that attribute. Exactly what are the values for the id and priority parameters? Does id have to be a unique guid, or does it signify something else? Thanks!

Tamas Volgyesi said...

Hi Hamid, I downloaded and built the sample project.
Then I installed the TeamExplorerExtSample.vsix file.
However I could not find among the navigation items in team explorer.
Could you please help?

Nithya Sri said...
This comment has been removed by the author.
Softgen Infotech said...

I can’t imagine that’s a great post. Thanks for sharing.

Learn SAP Training from the Industry Experts we bridge the gap between the need of the industry. Softgen Infotech provide the Best SAP ABAP Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.

eTechno Soft Solutions said...

Post is very useful. Thank you, this useful information.

Looking for SAP HANA ADMIN Training in Bangalore, learn from eTechno Soft Solutions SAP HANA ADMIN Training on online training and classroom training. Join today!

Softgen Infotech said...

Really very happy to say, your post is very interesting to read. I never stop myself to say something about it. You’re doing a great job. Keep it up…

Upgrade your career Learn Oracle Training from industry experts gets complete hands on Training, Interview preparation, and Job Assistance at Softgen Infotech.

Unknown said...

Hi Hamid,

I downloaded the project and it worked for me in visual studio 2017.
However, when I tried to make it on my own it did not work. I created a new guid and gave it but it did not work. I also tried to copping the complete code of yours but it didn't work.

Could you please help me in finding what went wrong in my case.
namespace TeamExplorerExtSample
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using Microsoft.TeamFoundation.Controls;

[TeamExplorerNavigationItem("3A46684A-70C9-4083-B04B-BE8A565C7FA2", 1800)]
public class NotepadNavigationItem : ITeamExplorerNavigationItem2
{
public bool IsEnabled => true;

public int ArgbColor => 0;

public object Icon => null;

public string Text => "Open Notepad++";

public Image Image => null;

public bool IsVisible => true;

public event PropertyChangedEventHandler PropertyChanged;

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
}

public void Execute()
{
Process.Start("notepad.exe");
}

public void Invalidate()
{
}
}
}

If it is not more to ask, could you please help me in adding a button to pending changes window so that I can create a review.

Anu said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
DevOps Training in Chennai | DevOps Training in anna nagar | DevOps Training in omr | DevOps Training in porur | DevOps Training in tambaram | DevOps Training in velachery

rocky said...

I like that your blog. This is excellent information. It is amazing and wonderful to visit your site.
Python Training in Chennai | Certification | Online Training Course | Python Training in Bangalore | Certification | Online Training Course | Python Training in Hyderabad | Certification | Online Training Course | Python Training in Coimbatore | Certification | Online Training Course | Python Training in Online | Python Certification Training Course


Asha Kaashiv said...

I am expecting more interesting topics from you. And this was nice content. click here for internship details Internship for MCA Students , Online Internship for MCA Students , Summer Internship for MCA Students , Internship for MSC Students , Online Internship for MSC Students , Summer Internship for MSC Students , Internship in Chennai for MSC Students , Msc Internship , Online MSC Internship , MSC Online Internship