Nuget Packages

Adding Configs from Nugets to Visual Studio PackageReference Projects

  • 27/07/2023
  • Technical
  • Sitecore XP, Third-party Integrations, Best Practices

NuGet is a package manager for the Microsoft development platform, primarily used for .NET applications. It simplifies the process of adding, updating, and managing third-party libraries and tools within your projects. With NuGet, developers can easily integrate external functionalities into their applications, saving time and effort.

What's the problem?

Did you ever want to install a nuget package which includes config files or other content and you want to have it visible in Visual Studio and also include it in your normal msbuild publish? If yes, I have an answer for you 😉

With the following steps, you can easily include config files from Nuget packages into your solution. I have done this on the example with the popular Sitecore Image Optimizer package Dianoga Version 6.1.0.

Also to mention, I am using the helix-autopublish from Anders Laub (github), so instead of web projects I only use class library projects in my case, but this should also work in newer sdk web projects with no problem.

So what's the solution now?

Here are the three simple steps to make the magic happen:

1. Add the Dianoga nuget package to your project.
2. Go to the location where your nuget was installed (usually in the user directory under .nuget) and look for the configs and copy the path starting from the package name. In my example \dianoga.6.1.0\content
3. Add the following to the csproj file of the project:


<ItemGroup>
    <Content Include="$(NuGetPackageRoot)\dianoga\6.1.0\content\**\*.*" />
</ItemGroup>
    

Voila! Your are done 🙂

The readonly confis are marked with a small arrow as you can see in the example picture:

Example of readonly config files from nuget packages

Additional Steps

If you now want to enable a config which is disabled by default, you still can create the same folder structure in the project and copy this one config there and rename it to enable it. In Visual Studio it will be visible in the same folder as the configs from the nuget, as it will be after the deployment in the web folder.

You can also see a visible difference from nuget included configs and those you included yourself by the arrow marker.

Happy coding! 🤓

P.S.: For more informations about Sitecore and our Services, please visit www.cyber-solutions.at