28 May, 2021

Sitecore Upgrade - Codebase Upgrade Series - Remove unused references and nuget packages

As part of the Sitecore codebase upgrade series, in this blog, we are going to deal with unused references and packages. It is worth to cleanup the unused references and nuget packages to reduce the complexity, dependencies and effort to upgrade to be compatible with target Sitecore version. 

Remove Unused References:

Visual Studio 2019 has the inbuilt "Remove Unused References" option for .NET framework projects with .NET SDK project format and .NET core projects. If the project type is already in .NET SDK format, then right click on project or on the references and select Remove Unused References. Then select the list of references to remove and click Apply. As of Visual Studio 2019 v16.10.0, this option is turned off by default. To enable it, you can go to menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer. 

Remove Unused Packages

To remove Unused packages, you may want to get ReSharper which can clean up the unused references and also remove the unused packages. If ReSharper is installed, you can refactor the entire solution or a particular project. Right Click on the Solution > Refactor > Remove Unused References. This opens up a window with the list of references and packages and you can select and remove it. This will remove the entry from Packages.config or from PackageReferences if it is used. 

There are other open source extensions in Visual Studio Gallery and few proprietary tools like CoreRush or Telerik® JustCode™ are available in the market which must be doing the same tasks. 

07 May, 2021

Visual Studio - PowerShell Package Manager Console Reload Profile Script

In case if you are setting up a NuGet PowerShell profile or modifying the profile script for Visual Studio PowerShell Package Manager Console, you will need to close and open the Visual Studio to reload the profile methods. 

To reload the profile scripts without closing and opening the Visual Studio, you can run the below command. This will reload the profile script and the updated method will be available for you to use in Console. 

. $profile

06 May, 2021

Sitecore Upgrade - Sitecore NuGet Package Upgrade using PowerShell

This blog is related to thSitecore codebase upgrade series.

Sitecore enabled package management system for the Sitecore packages from version 9.x. This has helped to avoid unnecessary and complicated reference management. In order to do .NET framework upgrade and Sitecore NuGet package upgrade, there are various ways as follows.
  1. Using NuGet Package Manager 
  2. Using PowerShell Package Manager Console
  3. NuGet CLI
  4. dotnet CLI
Certain Sitecore Helix solution may have 100s of projects so using NuGet Package Manager in Visual Studio may not be productive. As a programmer, you can leverage the PowerShell Package Manager Console to inspect each project and update the packages. 

Gary has written a script to update the Sitecore Packages and as he mentioned, it is easy to extend and tweak it as per our need. One of my requirement is to find the projects which are having Sitecore Packages and update all the packages to the target version in a single shot.  

The below script accepts the old package version and the new package version and finds all the projects having "Sitecore." packages and upgrades the packages to the target version along with dependencies. Save the gist file as nuget_Profile.ps1 in C:\Users\[user]\Documents\WindowsPowerShell folder and restart the Visual Studio for the PowerShell Package Manager console to pick up the profile scripts. 
  1. Finds all the projects with Sitecore packages
  2. Loop through each projects and updates the Sitecore Packages.
  3. If NoReference Package is installed, it finds the alternative package with NoReference and installs it. 
In the PowerShell Package Manager console, execute the method along with old and new package version as below. Please note script is defaulted to version 9.3.0 as old version and version 10.1.0 as the new version. 
UpdateSitecorePackagesForAllProjects -NewVersion 9.0.171219 -OldVersion 10.0.1

01 May, 2021

Sitecore Upgrade - Codebase Upgrade Series

One of the task in a Sitecore upgrade project is to upgrade the codebase to be compatible with the target Sitecore version. I have listed some of the steps which I learnt and it helped me to upgrade the solution quickly. 

  1. Remove unused references and nuget packages
  2. Framework upgrade - .NET
  3. Migrate to SDK Project Format (optional)
  4. Migrate from packages.config to PackageReference
  5. Replace Sitecore assemblies reference with nuget package
  6. Upgrade Sitecore nuget packages
  7. Upgrade .NET related nuget packages
  8. Upgrade 3rd party nuget packages
  9. Compile, replace obsolete methods and fix errors
Before upgrading the codebase, make sure to create a separate branch so that it does not affect the ongoing development tasks. 

Visual Studio PowerShell related articles to run scripts. 

Sitecore Upgrade related articles.

blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }