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. The Manage NuGet Packages menu command is registered as a standard VS command, thus you can call DTE.ExecuteCommand()
to invoke it. The trick is to know the command name, which is Project.ManageNuGetPackages. Here’s a code sample:
DTE dte = (DTE)GetService(typeof(SDTE));
dte.ExecuteCommand("Project.ManageNuGetPackages");
Note that it will open the project-level dialog. To open the solution-level dialog instead, you replace the command name with Tools.ManageNuGetPackagesforSolution.
The ExecuteCommand()
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:

If, for some reason, you don’t like to call through DTE, you can also invoke the command directly through the OleMenuCommandService
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 the NuGet source code.
We are excited to announce that NuGet 2.6 has been released and it is included in the box with Visual Studio 2013 Preview. For details about what’s in the release, the release notes are published on docs.nuget.org.
NuGet 2.6 Features
- Support for Visual Studio 2013
- XDT Support
- Machine-Wide Configuration
- Contextualizing Search
- Tracking Direct Installs vs. Dependency Installs
Support for Visual Studio 2013
NuGet 2.6 is the first release that provides support for Visual Studio 2013. And like Visual Studio 2012, the NuGet Package Manager extension is included in every edition of Visual Studio.
In order to provide the best possible support for Visual Studio 2013 while still supporting both Visual Studio 2010 and Visual Studio 2012, and keeping the extension sizes as small as possible, we are producing a separate extension for Visual Studio 2013 while the original extension continues to target both Visual Studio 2010 and 2012.
Starting with NuGet 2.6, we will publish two extensions as below:
- NuGet Package Manager (applies to Visual Studio 2010 and 2012)
- NuGet Package Manager for Visual Studio 2013
With this split, the nuget.org home page’s “Install NuGet” button will now take you to the installing NuGet page, where you can find more information about installing the different NuGet clients.
XDT Support
One of the most highly-requested features for the NuGet client has been to support more powerful XML transformations using the XDT transformation engine which is used in Visual Studio build configuration transformations.aspx). In April 2013, we made two big announcements regarding NuGet support for XDT. The first was that the XDT library itself was being itself released as a NuGet package and open sourced on CodePlex. This step enabled the XDT engine to be used freely by other open-source software, including the NuGet client. The second announcement was the plan to support use of the XDT engine for transformations in the NuGet client. NuGet 2.6 includes this integration.
Machine-Wide Configuration
As more companies are using NuGet internally, with private package sources, it has become important to apply machine-wide configuration for NuGet that can make it easy for developers to get access to these private package sources in addition to the official NuGet package source at http://nuget.org. Additionally, some development tools need to expose additional package sources for developers for easy access to packages related to those tools.
With NuGet 2.6, the configuration model has been extended to allow machine-wide package sources to be registered for developers to easily discover those package sources. This is a feature that Visual Studio 2013 itself utilizes to register a “.NET Framework Packages” feed by default.
Contextualizing Search
As the number of packages served by the NuGet gallery continues to grow at an exponential pace, improving search remains ever at the top of the NuGet priority list. One of the planned features for NuGet is contextual search, meaning that NuGet will use information about the version and SKU of Visual Studio that you are using and the type of project that you are building as criteria for determining the relevance of potential search results.
Starting with NuGet 2.6, each time a package is installed, the context for the installation is recorded as part of the installation operation data. Searches also send the same context information, which will allow the NuGet Gallery to boost search results by contextual installation trends. A future update to the NuGet Gallery will enable this context-sensitive relevance boosting.
Tracking Direct Installs vs. Dependency Installs
Package authors are relying more and more on the Package Statistics provided on the NuGet Gallery. One significant missing data point that authors have asked for is a differentiation between direct package installs and dependency installs. Until now, the NuGet client did not send any context around the installation operation for whether the developer directly installed the package or if it was installed to satisfy a dependency. Starting with NuGet 2.6, that data will now be sent for the installation operation. Package Statistics on the NuGet Gallery will expose that data as separate install operations, with a “-Dependency” suffix.
- Install
- Install-Dependency
- Update
- Update-Dependency
- Reinstall
- Reinstall-Dependency
In addition to the different operation name, the dependent package id is also recorded for the installation. A future update to the NuGet Gallery will expose that data within reports, allowing package authors to fully understand how developers are installing their packages.
Bug Fixes
NuGet 2.6 also includes several bug fixes. For a full list of work items fixed in NuGet 2.6, please view the NuGet Issue Tracker for this release.
One common misconception we see among NuGet users is that NuGet could only install/ update to the latest version of a package in VS. This is due to the fact that through ‘NuGet Package Manager Dialog’ we only show that latest (stable or prerelease) version of any package. But, NuGet provides the ability to install/ update to any specific version of a package through Package Manager Console. Let’s see an example below on how to do this.
Install a specific version of a package
To bring up Package Manager Console in VS, go to View->Other Windows->Package Manager Console.

You could use the highlighted dropdowns above to set the Package Source and the Project in which the package should be installed.
To install EntityFramework 4.3.1, you could type the following in the console.
Install-Package EntityFramework -Version 4.3.1
In addition to being able to install a specific version of a package, there are a few other things that could be done through Package Manager Console, that are not available through UI. Let’s see those features below.
Reinstall packages that have already been installed
There may be instances when you would like to install a package that is already installed. For example, in some build or CI scenarios, the package version may not be incremented but you may want to install the latest produced package. This cannot be done through the Package Manager Dialog as NuGet will notify that the package is already installed. However, through Package Manager Console you could reinstall an existing package. Let’s say you have SamplePackage 1.8 already installed and you want to reinstall it. You can use the following command to reinstall SamplePackage. Reinstall will always install the same version that was previously installed.
Update-Package SamplePackage -reinstall
Please note that -reinstall flag works only on NuGet Clients 2.1 and above
Ignore dependencies when installing a package
Another thing that you could do through the Package Manager Console but not through the Package Manager Dialog is installing a package without it’s dependencies. To do this, you could use the -ignoreDependencies switch with Install-Package command. For example, jQuery.Validation package has a dependency on jQuery. When you install through the Package Manager Dialog the dependencies will always get installed. However, executing the following command in Package Manager Console will only install jQuery.Validation and not jQuery.
Install-Package jQuery.Validation -ignoreDependencies
Force
The next thing that you could do with Package Manager Console easily is to force uninstall of a package that is being dependent upon by other packages. Let’s take the example of jQuery and jQuery.Validation in a project. When you try to uninstall jQuery from the project through the Package Manager dialog, you will see the following UI that tells you that it can’t be done.

In case of Package Manager Dialog, you have to first uninstall the packages that have jQuery dependency before you can uninstall jQuery itself. However, with Package Manager Console, you can use the -force switch to uninstall the jQuery package.
UnInstall-Package jQuery -force
Source
When you install NuGet, it automatically comes with one package source - the NuGet official package source. If you would like to install packages from other sources you need to go to Tools->Options->Package Manager->Package Sources to add other custom sources. But, if you would like to install a package from a custom source and not have it as one of your sources in your package sources, you can quickly do it through Package Manager Console using the -Source switch. An example is given below.
Install-Package MyPackage -Source PATH_TO_YOUR_CUSTOM_PACKAGE_SOURCE
The above are some of the differences between Package Manager Dialog and Package Manager Console and highlight how you could use the Package Manager Console to do some additional things that are not possible through the dialog. Have fun exploring the Package Manager Console!
-Ranjini