Generate NuGet packages for Sitecore releases via PowerShell


I’ve been working a lot on Sitecore development infrastructure recently and package management is one of the areas, particularly NuGet.

Unfortunately, Sitecore doesn’t provide any feed that you could use right away. so carrying over Sitecore DLLs in a repository is a standard practice that I’ve seen for few years and actually been using.

So I decided to create my own packages and here how I did it.


My goal was to create versioned packages for every version starting from 8.0. Once I started I found out that versioning of DLL and installation ZIP – two things not related one to each other. Major versions of DLLs were not in sync with release numbers. And I’m not even talking about update number, which is not there in any form.

After few attempts to use Sitecore DLL version, I gave up and come up with my own: MAJOR_VERSION.MINOR_VERSION.UPDATE.REVISION_DATE.

This structure is simple and intuitive. You don’t need to remember that update 3 to version 8.0 was released on Apr 27th, 2015.

Also, I’ve made following simplification – everything that is coming from one installation ZIP would have one version.


Generation has a few steps described in a PowerShell script:

  • find DLLs starting with Sitecore in website/bin folder;
  • generate version and description based on version;
  • go with Reflection into an assembly and detect dependencies to other Sitecore libraries;
  • detect the assembly framework and generate .nuspec file
  • generate NuGet package and push into your feed
[string] $Major = "8",
[string] $Minor = "1",
[string] $Update = "0",
[string] $Date = "151003",
[string] $nugetPath = ".\tools\nuget.exe",
[string] $downloadsPath = ".\downloads",
[string] $nuspecTemplate = ".\tools\package.nuspec.xml",
[string] $apiKey,
[string] $nugetFeed = "http://use-your.own/nuget/Sitecore-Libs"
function Publish-NugetPackageFromWebsiteBin(
[string] $major,
[string] $minor,
[string] $update,
[string] $date,
[string] $nugetPath,
[string] $downloadsPath,
[string] $nuspecTemplate,
[string] $apiKey,
[string] $nugetFeed)
$bin = Resolve-Path ("$downloadsPath\Sitecore $major.$minor rev. $date\website\bin")
$nugetPath = Resolve-Path $nugetPath
$nuspecTemplate = Resolve-Path $nuspecTemplate
$updateText = "Initial Release"
if($update -gt 0)
$updateText = "$major.$minor Update-$update"
$description = "Sitecore Experience Platform $major.$minor rev. $date ($updateText)"
$version = "$major.$minor.$update.$date"
# clean-up
$output = New-Item -ItemType Directory -Path ($env:temp + "\" + [System.Guid]::NewGuid())
#call functions
Get-ChildItem $bin |
Where-Object {$_.Name -match "^Sitecore.*\.dll$"} |
Foreach { Publish-NugetPackageFromDLL $bin $_.Name $output.FullName $description $version $nugetPath $nugetFeed $apiKey $nuspecTemplate }
Remove-Item $output -Force -Recurse
function Publish-NugetPackageFromDLL(
$binPath, $assemblyName, $output, $description, $productVersion, $nugetPath, $nugetFeed, $apiKey, $nuspecTemplate)
$tempPath = $output + "\tmp"
# dlls
$file = Get-Item "$binPath\$assemblyName"
$id = [System.IO.Path]::GetFileNameWithoutExtension($file)
$version = $productVersion
$framework = Get-FrameworkVersion($file)
# create package directory
$tmpFolder =New-Item $tempPath -type directory -Force
$packageFolder = New-Item "$tmpFolder\$id" -type directory -Force
$libsFolder = New-Item "$packageFolder\lib" -type directory -Force
$frameworkFolder = New-Item "$libsFolder\$framework" -type directory -Force
# packages
$nuspeck = New-Nuspec $file $binPath $nuspecTemplate $id $productVersion $description $framework $packageFolder
# write files
Copy-Item $file $frameworkFolder -Force
$assemblyXml = "$binPath\$id.xml"
if(Test-Path $assemblyXml)
Copy-Item $assemblyXml $frameworkFolder -Force
$assemblyPub = "$binPath\$"
if(Test-Path $assemblyPub)
Copy-Item $assemblyPub $frameworkFolder -Force
$output = New-Item "$basePath\packages" -type directory -Force
& $nugetPath pack $nuspeck.FullName -OutputDirectory $output
if (-Not([string]::IsNullOrEmpty($apiKey)) -AND -Not([string]::IsNullOrEmpty($nugetFeed))){
& $nugetPath push "$output\$id.$productVersion.nupkg" -ApiKey $apiKey -Source $nugetFeed
function New-Nuspec(
[string] $pathToDependencies,
[string] $nuspecTemplate,
[string] $packageId,
[string] $packageVerison,
[string] $packageDescription,
[string] $packageFramework,
[string] $output
$xml = [xml](Get-Content $nuspecTemplate)
# Set ID
$node = $xml.package.metadata
$node.ID = $packageId
$node.version = $packageVerison
$node.description = "Autogenerated NuGet package form $packageDescription"
$group = $xml.CreateElement('group');
# get framework version
$group.SetAttribute('targetFramework', $packageFramework )
$assemblyDependencies = Get-AssemblyDependencies($assemblyFile)
$dependencies = $xml.CreateElement('dependencies');
foreach ($assembly in $assemblyDependencies){
$depVersion = Find-AssemblyAndReturnVersion "$pathToDependencies\$($assembly.Name).dll" $packageVerison
if ($depVersion -ne "0.0.0")
$dep = $xml.CreateElement('dependency')
$dep.SetAttribute('id', $assembly.Name )
$dep.SetAttribute('version', '[' + $(Find-AssemblyAndReturnVersion "$binPath\$($assembly.Name).dll" $packageVerison) + ']')
# write nuspeck
return Get-Item "$output\$packageId.nuspec"
function Get-FrameworkVersion([string] $path)
$imageRuntimeVersion = [System.Reflection.Assembly]::LoadFrom($path).ImageRuntimeVersion.ToString()
return "net45"
return "net40"
function Get-AssemblyDependencies([string] $path)
return [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($path).GetReferencedAssemblies() |
Where-Object {$_.Name -match "Sitecore\.[A-Za-z\.]*"} |
Select Name |
Where { -not([string]::IsNullOrEmpty($_)) } |
Sort-Object -Property Name |
function Find-AssemblyAndReturnVersion([string] $pathToAssembly, [string] $productVesion)
if (Test-Path $pathToAssembly)
return $productVesion
return "0.0.0"
Publish-NugetPackageFromWebsiteBin $Major $Minor $Update $Date $nugetPath $downloadsPath $nuspecTemplate $apiKey $nugetFeed


Once packages are generated and pushed to a NuGet repository we have the following picture. Enjoy!


Follow me on twitter @true_shoorik. Would be glad to discuss ideas above in comments.


3 thoughts on “Generate NuGet packages for Sitecore releases via PowerShell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s