none
Install a Nuget package programmatically RRS feed

  • Question

  • I found this blog post by the Nuget team showing how you can install a nuget package programmatically. I've essentally created the same thing in my code however I want to install to the default $(SolutionDir)\packages folder.

    // Install the TemplateBuilder Nuget pkg into the target project
    string packageID = "TemplateBuilder";
    IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");
    
    string installPath = ""; // Somehow need to get $(SolutionDir)\packages of the target project
    PackageManager packageManager = new PackageManager(repo, installPath);
    packageManager.InstallPackage(packageID);
    

    Does anyone know how to set the path as the default location?



    • Edited by RandomlyKnighted2 Tuesday, March 31, 2015 2:35 AM Fixed question after looking finding additional documentation
    Tuesday, March 31, 2015 12:42 AM

Answers

  • It took another full day of digging through the documentation but I finally figured it out. Turns out Nuget.Core was not what I was looking for. I needed the IVsPackageInstaller interface from the Nuget Extensibility APIs, which you can find documentation for here. You can also find out how to use this API by using the information found here. For anyone interested here's how I was able to install the package:

    private bool InstallTemplateBuilderPackage(EnvDTE.Project project)
    {
          bool installedPkg = true;
          try
          {
                var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
                IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>();
    
                if (!installerServices.IsPackageInstalled(project, "TemplateBuilder"))
                {
                      var installer = componentModel.GetService<IVsPackageInstaller>();
                      installer.InstallPackage("All", project, "TemplateBuilder", (System.Version)null, false);
                }
          }
    
          catch (Exception ex)
          {
                installedPkg = false;
    
                // Log the failure
          }
    
          return installedPkg;
    }

    Wednesday, April 1, 2015 2:29 AM

All replies

  • It took another full day of digging through the documentation but I finally figured it out. Turns out Nuget.Core was not what I was looking for. I needed the IVsPackageInstaller interface from the Nuget Extensibility APIs, which you can find documentation for here. You can also find out how to use this API by using the information found here. For anyone interested here's how I was able to install the package:

    private bool InstallTemplateBuilderPackage(EnvDTE.Project project)
    {
          bool installedPkg = true;
          try
          {
                var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
                IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>();
    
                if (!installerServices.IsPackageInstalled(project, "TemplateBuilder"))
                {
                      var installer = componentModel.GetService<IVsPackageInstaller>();
                      installer.InstallPackage("All", project, "TemplateBuilder", (System.Version)null, false);
                }
          }
    
          catch (Exception ex)
          {
                installedPkg = false;
    
                // Log the failure
          }
    
          return installedPkg;
    }

    Wednesday, April 1, 2015 2:29 AM
  • Very helpful, thanks!
    Wednesday, April 1, 2015 3:22 AM
  • Hi ,

    I need install NuGet to Website project. In that case i can't use the following

    installer.InstallPackage("All", project, "TemplateBuilder", (System.Version)null, false);

    Since Website project din't have any project object.So how to proceed further in this situation.


    Kani

    Wednesday, June 29, 2016 6:08 AM