GitHub Copilot is now available for free

No trial. No credit card required. Just your GitHub account.

July 25th, 2013
0 reactions

Invoke the Manage NuGet Packages dialog programmatically

Recently I’ve been asked several times for how to invoke the Manage NuGet Packages dialog from VS components programmatically, and optionally auto-invoke the search when the dialog opens. Today I’d like to share the solution in this post.

It’s actually very straightforward. TheManage NuGet Packages menu command is registered as a standard VS command, thus you can callDTE.ExecuteCommand() to invoke it. The trick is to know the command name, which isProject.ManageNuGetPackages. Here’s a code sample:

DTE dte = (DTE)GetService(typeof(SDTE)); dte.ExecuteCommand("Project.ManageNuGetPackages");

Note that it will open theproject-level dialog. To open thesolution-level dialog instead, you replace the command name withTools.ManageNuGetPackagesforSolution.

TheExecuteCommand() method also accepts a second optional parameter of string type. If you pass a string value to it, the NuGet command handler will intepret it as a search query and will automatically issue a search request after it has opened the dialog.

DTE dte = (DTE)GetService(typeof(SDTE)); dte.ExecuteCommand("Project.ManageNuGetPackages", "jQuery");

With the above code, assuming you have the Online tab selected the last time you closed it, you will see this:

Dialog opened with search query

If, for some reason, you don’t like to call through DTE, you can also invoke the command directly through theOleMenuCommandService service.

var nugetCommandGuid = Guid.Parse("25fd982b-8cae-4cbd-a440-e03ffccde106"); var nugetCommand = new CommandID(nugetCommandGuid, 0x100); // 0x100 is the command id for project-level dialog OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; mcs.GlobalInvoke(nugetCommand, "jQuery");

Again, to open the solution-level dialog, you set the command id to 0x200. These constants are set in theNuGet source code.

Share

0 comments

Discussion is closed.

    Stay informed

    Get notified when new posts are published.
    Follow this blog
    facebooklinkedinyoutubetwitchStackoverflow