Wednesday, 31 August 2011

How to write a SQL Server Management Studio Add-in for Denali (CTP3) (Part 2)

In my previous post on writing a SSMS Add-in for Denali I detailed the steps that you would need to take to get the Add-in loaded and running but it didn’t do a great deal.

Now we take it a step further. But not too far(!)

I’ve done a number of things;


1) As we all know, SSMS Add-in’s only work because of a deep dark magic, but there are a few ways we can peek inside to see what’s happening.  


The DTE object we get during the OnConnection method has a class full of events – so lets hook them up and see what happens.



  EnvDTE.TextDocument doc = (EnvDTE.TextDocument)ServiceCache.ExtensibilityModel.Application.ActiveDocument.Object(null);


Returns a TextDocument object from which you can get the contents of the window (SQL).


3) Just like previous versions add-ins, you use the same code to pick up Object Explorer events.

   1:                 objExplorerService = (ObjectExplorerService)ServiceCache.ServiceProvider.GetService(typeof(IObjectExplorerService));
   2:                  cs = (ContextService)objExplorerService.Container.Components[0];
   3:                  cs.ObjectExplorerContext.CurrentContextChanged += new NodesChangedEventHandler(ObjectExplorerContext_CurrentContextChanged);

Download the source from codeplex, check it out, play with it, check it in!