VsixCommunity / Community.VisualStudio.Toolkit

Making it easier to write Visual Studio extensions
Other
249 stars 44 forks source link

Server Explorer custom context menu item #498

Closed ErikEJ closed 2 months ago

ErikEJ commented 2 months ago

I am trying to get the ADO Connection object from an item selected in Server Explorer.

I found this old thing - but is there a better way with Vsix Toolkit?

https://stackoverflow.com/questions/15541438/how-to-get-the-selected-connection-node-object-of-vs-server-explorer-window-ddex

(I am fine with just getting the name of the selected node)

ErikEJ commented 2 months ago

I ended up with this relativly unbrittle implementation:

            var Dte2 = GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
            var uih = Dte2.ToolWindows.GetToolWindow(EnvDTE.Constants.vsWindowKindServerExplorer) as EnvDTE.UIHierarchy;
            var selectedItems = (Array)uih.SelectedItems;

            if (selectedItems != null)
            {
                var connectionName = ((EnvDTE.UIHierarchyItem)selectedItems.GetValue(0)).Name;

                var dataConnectionsService = await VS.GetServiceAsync<IVsDataExplorerConnectionManager, IVsDataExplorerConnectionManager>();
                if (dataConnectionsService.Connections.TryGetValue(connectionName, out IVsDataExplorerConnection explorerConnection))
                {
                    var connection = explorerConnection.Connection;

                    if (VsDataHelper.SupportedProviders.Contains(connection.Provider))
                    {
                        menuCommand.Visible = true;
                    }
                }
            }