Recently, I started the migration of a Sitecore project from 9.1 to 10.1. I faced the problem of updating the Sitecore libraries to a higher version in a Visual Studio solution with around 100 projects. I was going to spend a lot of time If I did it manually. Hopefully, I found this blog that helped me to complete that task automatically.
First, below is the entry main Powershell function. That gets the projects and packages recursively. Each package evaluates if that starts with the word Sitecore and gets the new version from NuGet to be updated in the project.
function my - nuget - upgradeSitecore - all - projects([string] $OldVersion,
[string] $NewVersion,
[string] $Source = "https://sitecore.myget.org/F/sc-packages/api/v3/index.json") {
$projFiles = Get - ChildItem $dir - Recurse - Filter *.csproj
foreach ($file in $projFiles)
{
$projFileFinal = $file - replace '.csproj', ''
Write - Host "$projFileFinal" - BackgroundColor Blue
$pkgs = Get - Package Sitecore. -ProjectName $projFileFinal
foreach ($pkg in $pkgs)
{
if ($pkg.Version - eq $OldVersion) {
$toInstall = $pkg.Id.Replace(".NoReferences", "")
Write - Host "Package to update: $toInstall" - BackgroundColor DarkMagenta
$exists = Find - Package - Id $toInstall - Source $Source - Version $NewVersion - ExactMatch
if ($exists - eq $null) {
Write - Host "$toInstall does not exist... `n Upgrade $toInstall manually..." - BackgroundColor Blue
}
else
{
Update - Package $toInstall - Version $NewVersion - ProjectName $projFileFinal - Source $Source
}
}
}
}
}
You can download the complete script here. Additionally, you have the following additional utilities/functions.
my-nuget-upgradeSitecore-all-projects-package that updates a Nugget specific package in all solution projects.
Second, below are the indications to install the PowerShell script into your Visual Studio 2019
- Download the PowerShell script here
- Copy the PowerShell script in C:\Users\[user]\Documents\WindowsPowerShell folder.
- Reopen the Visual Studio application, so that the Nugget console can load the new script.
- Open the Package Manager console and your new script is ready to be used.
Third, below are the instructions for usage. Execute the PowerShell function [my-nuget-upgradeSitecore-all-projects-package] command with the following parameters:
- [NewVersion] parameter is the new version of the library to be updated
- [OldVersion] parameter is the current version of the library in the Visual Studio solution
- [Package] parameter is the name of the NuGet package to be updated.
Here is an example to update all Nugget packages that start with the Sitecore word on the whole VS solution.
my-nuget-upgradeSitecore-all-projects-package -NewVersion 10.1.1 -OldVersion 9.1.1 -Package Sitecore.Kernel.
Here is an example to update all Nugget packages that start with the Sitecore word and match with the package name as input parameter on the whole VS solution.
my-nuget-upgradeSitecore-all-projects-package -NewVersion 10.1.1 -OldVersion 9.1.1 -Package Sitecore.Kernel.
I hope this blog has been useful.