Visual Studio Extensibitity project - How to add a nuget package programmatically?

Topics: General
Dec 24, 2014 at 7:51 PM
Here's what I'm trying to do, with a given "newlyCreateProject" of EnvDTE.Project type.

var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var packageInstaller = componentModel.GetService<IVsPackageInstaller>();
nuGetRepo = ??????? HOW TO GET ONE ??????
packageInstaller.InstallPackage(nuGetRepo, newlyCreatedProject, "nUnit", "2.6.3", false, false);

I tried
var nuGetRepo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
but docs say we are not to reference the NuGet.Core, only reference NuGet.VisualStudio. PackageRepositoryFactory is a type in NuGet.Core.

The last line throws an exception in that case.

Best,

Friedrich
Dec 25, 2014 at 5:04 PM
OK, took a look at the source, which answered the question (you pass in null for repository)

Project project; // e.g from _dte.Solution.Projects.Item(0);
var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var packageInstaller = componentModel.GetService<IVsPackageInstaller>();
packageInstaller.InstallPackage(null, newlyCreatedProject, "nUnit", new Version(2,6,3), false);