João Neto's Blog

João Neto's Blog

Save time updating Sitecore Nuget Packages

Save time updating Sitecore Nuget Packages

Subscribe to my newsletter and never miss my upcoming articles

This is a spin-off of a devlog I started some time ago. Today, I am sharing a simple Powershell script to help you with the "Update Sitecore NuGet Packages" task.

If you have been following me along with this series, you’ve noticed that one of the tasks is updating Nuget Packages to a later version. The process of updating Nuget Packages is tedious and repetitive, besides it can take too long especially if you have 125 projects.

In a nutshell, here’s what you have to do:

  1. Right-click the project/solution.
  2. Click Manage Nuget Packages… / Manage Nuget Packages for the solution.
  3. Click the Updates tab.
  4. Select an installed Sitecore.* package.
  5. In the Install and Update Options, set the Ignore Dependencies option in the Dependency Behavior field.
  6. Click Update
  7. Repeat this process for every Sitecore.* package and project.

This process gets even more challenging if you are using those Sitecore.*.NoReferences packages. They were abandoned on Sitecore release 9.1 (and later). You have to uninstall them first and install the corresponding ones without the .NoReferences suffix.

Manage Nuget Packages.png

Let’s see how we can in one go update the packages to the latest version and remove those *.NoReferences packages. Open the Package Manager Console (In Visual Studio 2019, Tool > Nuget Package Manager > Package Manager Console) and run the following script:

PM> Write-Host "Installing new packages and removing *.NoReferences packages...\n"; Get-Project -All | ForEach-Object { Get-Package -ProjectName $_.ProjectName } | Where-Object { $_.Id -like 'Sitecore.*' } | ForEach-Object { Uninstall-Package -Id $_.Id -ProjectName $_.ProjectName -Force ; Install-Package -Id $_.Id.Replace('.NoReferences', '') -IgnoreDependencies -ProjectName $_.ProjectName; }

A brief explanation of what this script is doing:

  • Get a list of the Sitecore NuGet packages installed in every project. This is the part Get-Project -All | ForEach-Object { Get-Package -ProjectName $_.ProjectName } | Where-Object { $_.Id -like 'Sitecore.*' }.
  • Loops through each project and uninstall the NuGet Package. We do this to remove the *.NoReferences dependencies. ForEach-Object { Uninstall-Package -Id $_.Id -ProjectName $_.ProjectName -Force ;.
  • Now, install the latest version available. Install-Package -Id $_.Id.Replace('.NoReferences', '') -IgnoreDependencies -ProjectName $_.ProjectName; }.
  • If you want to install a version lower than the latest, then use the -Version 9.2.0 parameter in the Install-Package command.

You might be wondering why I haven’t broken this script into multiple lines. First, because line continuation is not supported at the moment. See https://github.com/NuGet/Home/issues/8010. Second, although Visual Studio 2019 supports breaking lines by typing Ctrl + Shift + Enter, I prefer having something that I can copy and paste and run in one go.

Once the script above is finished, you’ll still see some old packages around, such as Sitecore.WFFM.Abstractions and Sitecore.NVelocity (those were removed in later releases). To remove those, we run a second script where we define which versions we want to keep.

PM> $targetVersions = @('10.1.0', '5.0.0'); Get-Project -All | ForEach-Object { Get-Package -ProjectName $_.ProjectName } | Where-Object { $_.Id -like 'Sitecore.*' } | ForEach-Object { if( $targetVersions -notcontains $_.Versions[0] ) { Uninstall-Package -Id $_.Id -ProjectName $_.ProjectName -Force  } }

Notice the $targetVersions = @('10.1.0', '5.0.0'); part. Where am I getting those? To see the latest updates available per project and package run the following command:

PM> Get-Package -Updates -Source 'https://sitecore.myget.org/F/sc-packages/api/v3/index.json'

You should get an output like the following:

Id                                  Versions  Description                                   ProjectName
--                                  --------  -----------                                   -----------
Sitecore.Framework.Conditions       {5.0.0}   Code contracts and conditions for the Site... Customer.Website
Sitecore.Abstractions               {9.2.0}   Package Description                           Customer.Foundation.Search
Sitecore.ContentSearch              {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.ContentSearch.Data         {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.ContentSearch.Linq         {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.Kernel                     {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.Logging                    {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.Logging.Client             {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.NVelocity                  {9.2.0}   Package Description                           Customer.Foundation.Search
Sitecore.Zip                        {10.1.0}  Package Description                           Customer.Foundation.Search
Sitecore.Abstractions               {9.2.0}   Package Description                           Customer.Feature.PageContent
Sitecore.ContentSearch              {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.ContentSearch.Data         {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.ContentSearch.Linq         {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.Framework.Conditions       {5.0.0}   Code contracts and conditions for the Site... Customer.Feature.PageContent
Sitecore.Kernel                     {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.Logging                    {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.Logging.Client             {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.NVelocity                  {9.2.0}   Package Description                           Customer.Feature.PageContent
Sitecore.XA.Feature.Navigation      {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.Abstractions {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.IOC          {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.Mvc          {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.Presentation {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.Rendering... {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.SitecoreE... {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XA.Foundation.Variants.... {10.1.0}  Sitecore Experience Accelerator 8.0.0.3751    Customer.Feature.PageContent
Sitecore.XConnect                   {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.XConnect.Search            {10.1.0}  Package Description                           Customer.Feature.PageContent
Sitecore.Zip                        {10.1.0}  Package Description                           Customer.Feature.PageContent

From the output above, we can see some dependencies were updated up to version 9.2.0 only (those should simply be deleted) and there are others that got a different version such as Sitecore.Framework.Conditions with its latest release being 5.0.0. With that in mind, we know which packages should be updated, and which ones should be deleted.

That should save you some time. Now, you can move to the not-so-fun part where you have to fix the code that has stopped working due to breaking changes in the Sitecore API.

Credits

Photo by Cats Coming from Pexels.

#cms
 
Share this
Proudly part of