Pages

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.  

events

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

 

 

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


activeDocument

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!


http://ssmsaddindenali.codeplex.com/

5 comments:

  1. I am having problem when i downloaded your source code and trying to use it please assist thank you.
    Here is the screen shot
    https://picasaweb.google.com/109227880271946894828/April82012

    ReplyDelete
    Replies
    1. I've got the same problem. Did You solve it?

      Delete
  2. Excellent post. I'm implementing something similar to what you have in the Source Controled version. I'm adding a pair of items to the object explorer for databases. So far it is kind of working there is one segment that is giving me trouble in two ways. This is also in SQL Sever 2012

    Below is the problem code.

    INodeInformation node = objectExplorer.FindNode(navigationContext.Context);
    if(node.Parent.InvariantName.Equals("Databases"))
    {
    if (_databaseMenu == null)
    {
    string value = typeof(IMenuHandler).ToString();

    _databaseMenu = (HierarchyObject)node.GetService(typeof(IMenuHandler));
    String wtf = _databaseMenu.ToString();
    RestoreMenuItem rst = new RestoreMenuItem();
    _databaseMenu.AddChild(string.Empty, rst);
    }

    }

    This works but it gives me two entries in the context menu because I think the call is being made aschynchronously twice to the change method. This is a nuisance but I do have a way around it. The other problem is more severe the FindNode method depending on how you are clicking around in management studio can seemingly randomly take forever. It does finish eventually but much later. Do you know of any work arounds for those problems?

    ReplyDelete
    Replies
    1. I am also having major issues trying to compile a working addin. I believe I have referenced the proper libraries but when I try to implement ObjectExplorerService, It throws an error "Unable to cast object of Type Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService to type Microsoft.SqlServer.Management.SqlStudio.Explorer.ContextService"

      I am hoping the community will share info on a working Explorer interface!

      Delete
    2. Did you solve FindNode problem ?

      Delete