The Solution Packager is a very useful tool that has been released with Dynamics CRM 2011 Update Rollup 10 and is currently also available in Dynamics CRM 2013 Sdk. Traditionally you can only move CRM components around between two environments using CRM Solutions. You will soon start to see the limitations of this if you are doing one of the below:
- You are working on a team where each developer has their own virtual machine or CRM organization for isolation purposes
- You are on a project with one or more parallel streams of work where each stream has their own environment and different release schedule but working on the same CRM Solution
In all of the above cases you will need to bring all the pieces together once the work is ready and release them into your main CRM environment for testing or further packaging as CRM solutions. You will find that the only way to merge these changes with the existing solution is to place your changes into a new/existing solution and deploy that to your main CRM environment that contains the existing changes (Note these could have moved on since you started to work your specific feature). Below are a few issues you might encounter with that approach:
- When you deploy the CRM Solution containing your changes this might potentially override customizations that are already in there. You might end up re-doing some of work manually in two places.
- It is difficult to track what changes were made to implement that specific feature. You can track your source control items such as JavaScript files and CSharp code files but not your CRM customisations such as changes to data model, workflows, user interface and others.
The solution packager allows to extract your customizations from your own/team environment after you are done with a feature and check-in your changes into source control along with changes you have made to the files that are normally in source control such as JavaScript & other code files for your plug-ins for example. By default when you run the extract operation this will include everything that is contained in the solution zip files so it will include things like your plug-in assemblies and JavaScript that you already have in source control. The solution packager allows you to provide a mapping file so that during the extract operation it can skip the files you already have in source control and use the files you already have in source control and use the newly complied assemblies from the latest source code. Below is a diagram so you can picture this.
To help make this easier I have created a sample PowerShell script that automates some of the activities discussed above.This is included in the latest xRM CI Framework along with the required assemblies. This script will do the below.
- Check-out your existing extracted files from TFS (optional)
- Export your managed and unmanaged solution from CRM
- Find the deltas and mark the files under TFS with add/delete/check-out accordingly
- Check-in the updated files back into TFS (optional)
You can use the sample script below and make any changes as required. Don’t forget to download and install the assemblies for the xRM CI Framework or plug-in your own utilities.
- # Filename: ExtractCustomizations.ps1
- param([string]$solutionPackager, #The full path to the solutionpackager.exe
- [string]$solutionFilesFolder, #The folder to extract the CRM solution
- [string]$mappingFile, #The full path to the mapping file
- [string]$solutionName, #The unique CRM solution name
- [string]$connectionString, #The connection string as per CRM Sdk
- [switch]$checkout, #Optional – pass if you want to Check Out the existing files in the extract location
- [switch]$checkin) #Optional – pass if you want to Check In the updated files after extraction
- $ErrorActionPreference = “Stop”
- Write-Host “Solution Packager: $solutionPackager”
- Write-Host “Solution Files Folder: $solutionFilesFolder”
- Write-Host “Mapping File: $mappingFile”
- Write-Host “ConnectionString: $connectionString”
- Write-Host “Check In: $checkin”
- Write-Host “Check Out: $checkout”
- # CI Toolkit
- $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
- $xrmCIToolkit = $scriptPath + “\..\PowerShell Cmdlets\Xrm.Framework.CI.PowerShell.dll”
- Write-Host “Importing CIToolkit: $xrmCIToolkit”
- Import-Module $xrmCIToolkit
- #TF.exe
- $tfCommand = “C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf.exe”
- #Check Pending Changes
- $pendingChanges = & “$tfCommand” status /recursive /noprompt “$solutionFilesFolder”
- if ($pendingChanges -like “*no pending changes*”)
- {
- Write-Output $pendingChanges
- }
- else
- {
- Write-Output $pendingChanges
- Write-Error “Pending Changes Detected. Undo your changes and try again.”
- return
- }
- #Before Files
- [string[]]$beforeFiles = [System.IO.Directory]::GetFileSystemEntries($solutionFilesFolder, “*”, [IO.SearchOption]::AllDirectories)
- Write-Host “Before Files: ” $beforeFiles
- #Check Out
- if ($checkout -and ($beforeFiles.Length -gt 0))
- {
- & “$tfCommand” checkout /recursive /noprompt “$solutionFilesFolder”
- }
- #Export Solutions
- Write-Host “Exporting Solutions to: ” $env:TEMP
- $unmanagedSolution = Export-XrmSolution -ConnectionString $connectionString -Managed $False -OutputFolder $env:TEMP -UniqueSolutionName $solutionName
- Write-Host “Exported Solution: $unmanagedSolution”
- $managedSolution = Export-XrmSolution -ConnectionString $connectionString -Managed $True -OutputFolder $env:TEMP -UniqueSolutionName $solutionName
- Write-Host “Exported Solution: $managedSolution”
- #Solution Packager
- $extractOuput = & “$solutionPackager” /action:Extract /zipfile:”$env:TEMP\$unmanagedSolution” /folder:”$solutionFilesFolder” /packagetype:Both /errorlevel:Info /allowWrite:Yes /allowDelete:Yes /map:$mappingFile
- Write-Output $extractOuput
- if ($lastexitcode -ne 0)
- {
- throw “Solution Extract operation failed with exit code: $lastexitcode”
- }
- else
- {
- if (($extractOuput -ne $null) -and ($extractOuput -like “*warnings encountered*”))
- {
- Write-Warning “Solution Packager encountered warnings. Check the output.”
- }
- }
- #After Files
- [string[]]$afterFiles = [System.IO.Directory]::GetFileSystemEntries($solutionFilesFolder, “*”, [IO.SearchOption]::AllDirectories)
- Write-Host “After Files: ” $afterFiles
- #Get the deltas
- $deletedFiles = $beforeFiles | where {$afterFiles -notcontains $_}
- $addedFiles = $afterFiles | where {$beforeFiles -notcontains $_}
- if ($deletedFiles.Length -gt 0)
- {
- Write-Host “Deleted Files:” $deletedFiles
- $_files = [System.String]::Join(“”" “”", $deletedFiles)
- & “$tfCommand” undo /noprompt “$_files”
- & “$tfCommand” delete /noprompt “$_files”
- }
- if ($addedFiles.Length -gt 0)
- {
- Write-Host “Added Files:” $addedFiles
- $_files = [System.String]::Join(“”" “”", $addedFiles)
- & “$tfCommand” add /noprompt “$_files”
- }
- # Checkin
- if ($checkin)
- {
- & “$tfCommand” checkin /noprompt /recursive /override:”PowerShell Extract” “$solutionFilesFolder” /bypass
- }
- # End of script
Below is also a sample script that invokes the script above with some predefined parameters to get you started.
- & “C:\Program Files (x86)\Xrm CI Framework\CRM 2013\PowerShell Cmdlets\ExtractCustomizations.ps1″ -solutionPackager “C:\workspace\CISample\Src\Sample2\Lib\CrmSdk\solutionpackager.exe” -solutionFilesFolder “C:\waelcrm2013\CISample\Src\Sample2\SolutionFiles” -mappingFile “C:\workspace\CISample\Src\Sample2\LocalSolutionPackagerMapping.xml” -solutionName “Sample” -connectionString “ServiceUri=http://myapp.cloudapp.net:5555/Sample2Dev/XRMServices/2011/Organization.svc; Domain=domain; UserName=username; Password=******” -checkout
If you this approach and you are using TFS for managing your backlog, I would strongly recommend you associated your check-in of the CRM Customizations (Extracted using the above) and any source files developed in visual studio with your product backlog item. This way you can later see what were the changes that were done to meet that specific requirement or bug.
Having said all of the above, parallel development adds an overhead and additional complexity. So only go for that option if you really need to. If you decide to ahead anyway, avoid making changes to the same customizations and components by different team members/teams and remember communication and a good process in place is always important. Bare in mind you can still use the solution packager for keeping a backup or your customizations and viewing deltas overtime which can be helpful in troubleshooting as well.
In the next post I will showing you how to automate the build of these extracted solution files and produce solutions that you can then deploy to other environments or ship to your customers.
