Installing NuGet packages

This post describes three examples of installing /re-installing NuGet packages. That is:

  • Re-install a NuGet package manually
  • Install NuGet packages from NuGet, via a PowerShell script
  • Install NuGet packages from a local artifact feed, via a PowerShell script & Visual Studio

1.) Re-install a NuGet package manually

a.) Connect to NuGet

Figure 1

Setup Visual Studio so that it can connect to NuGet

Figure 2

b.) Determine which version of the package is required to be re-installed

Figure 3

c.) Install the package

Figure 4

The missing package is then placed in the packages folder i.e. …\packages\Microsoft.CrmSdk.CoreAssemblies.9.0.2.5\lib\net452\Microsoft.Crm.Sdk.Proxy.dll & Microsoft.Xrm.Sdk.dll

2.) Install NuGet packages via a PowerShell script

a.) Connect to NuGet

Packages can also be downloaded from NuGet by executing the following PowerShell script ‘Install packages.ps1’.

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = ".\nuget.exe"
if((Test-Path -Path $targetNugetExe) -eq $false)
{
    Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
}
Set-Alias nuget $targetNugetExe -Scope Global
./nuget restore packages.config -PackagesDirectory packages

The script downloads nuget.exe if it doesn’t already exist on the file system. It then downloads any packages that are listed in packages.config

Figure 5

b.) Connect to a local artifact feed

Packages can also be created, stored and downloaded from a local Artifact feed.

In this example:

  • the local Artifact feed contoso-common-packages contains the NuGet package contosoCmdlets
Figure 6

The location of the Artifact feed is displayed in Figure 7

Figure 7
  • this NuGet package can be downloaded by executing the following PowerShell script ‘Install Contoso Tools.ps1’.
$version = "0.0.1.13";
$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = ".\nuget.exe"
if((Test-Path -Path $targetNugetExe) -eq $false)
{
    Write-Host "Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe"
    Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
}
Set-Alias nuget $targetNugetExe -Scope Global
Write-Host "./nuget install ContosoCmdlets -Version $version -Source https://pkgs.dev.azure.com/.../_packaging/contoso-common-packages/nuget/v3/index.json -OutputDirectory ./packages"
./nuget install ContosoCmdlets -Version $version -Source https://pkgs.dev.azure.com/.../_packaging/contoso-common-packages/nuget/v3/index.json -OutputDirectory ./packages

  • the NuGet package can also be downloaded in Visual Studio. This can be achieved by adding the artifact feed contoso-common-packages as a ‘Package Sources’ in the ‘NuGet Package Manager’. That is, adding another Package Source in Figure 2

References

Get started with NuGet packages in Azure Artifacts