Looking for the Unicorn

When working on a client-server oriented environment with Sitecore, you will have to develop your Sitecore Items locally, and send them to the server. Of course, you will use the “Publish” feature in Sitecore at first. But then, you will certainly need to edit your items and compare them with the precedent versions for example. That’s why you will need to serialize your items and integrate them in your favourite source control software…

A fabulous and mysterious creature appears in front of you : The unicorn.

Dabbing unicorn

 

I. Installing Unicorn on your local Sitecore instance

• Open your Visual Studio Project as an adminstrator

• Open the Package Manager Console (Tools>Nuget PackageManage>Package Manager Console)

• Install the Unicorn Nuget Package by using the command :

• Install-Package Unicorn

• Go to the App_Config/Include/Unicorn Folder.

• Rename “Unicorn.Configs.Default.example” to “Unicorn.Configs.Default.config”.

• Edit the file with your favourite text editor.

In this file, (within the <configuration> and <predicate> nodes) you need to signal the items that you want to synchronise or not. You can exclude the files you don’t need.

For example :


• Rebuild and Deploy your Visual Studio solution
.
• Go to the following URL : http://yoursitecoreinstance/unicorn.aspx . Log-in with your admin credentials, to get to this page :

• Click on the “Reserialize” button.

The Unicorn console opens up and creates for the first time all the .yml files, representing each items that you wanted to synchronise.


If everything goes well, go back to the configuration page.

• Click on the “Sync” button this time

The same console is displayed, and generates the items. You should now get all the items you wanted in your folder :


That’s all !!! When you edit a Sitecore item and want to keep a trace of it, just click on the Sync button. The files generated are in .yml extension and can be read by any existing text editor.

 

II. Create an automated deployment script for your items

A. Prepare the client-server communication

The next step that get us closer to a full automated Sitecore/Unicorn server, would be to set-up an deployment script.

Unicorn has already delivered for us these scripts right here :
https://github.com/kamsar/Unicorn

• You can choose to download the whole archive or only the folder named “doc / PowerShell Remote Scripting /”

Put the three files in the same folder, somewhere on your local disk.

• Open the file “sample.ps1”. I renamed it to “UnicornRemoteSync.ps1”.

• Edit this line and replace the url by the address of your remote server :

• Generate yourself a secret token. I used the website http://passwordsgenerator.net/ to get a 100 length token. It should be enough
.
• Copy the token, and paste it to the configuration line
.
• Go on your remote server, in your Website folder, in “App_Config\Include\Unicorn”.

Edit the Unicorn configuration file : “Unicorn.UI.config”, and paste your secret token in the “SharedSecret” tag :

You have now configured the connection between your remote server and your client. You just have to prepare the connection by signing your powershell scripts.

B. Sign your powershell scripts on your local machine

For this operation, I create another powershell file to separate execute my commands :


• Change your signature execution policy to ‘Unrestricted’ :

Set-ExecutionPolicy Unrestricted

• Use makecert.exe to create your own Certificate Authority

In Windows 10, this program is located at 'C:\Program Files (x86)\Windows Kits\10\bin\x64'.

./makecert.exe -pe -n "CN=versusmind" -ss My -a sha1 -eku 1.3.6.1.5.5.7.3.3 -iv versusmind.pvk -ic versusmind.cer

• Open your certificate LocalMachine Certificate store (mmc.exe)

• Develop the TreeList :

Go in the “Trusted publishers” folder and import the certificate that we have generated in the previous step.(All Tasks > Import… > Browse in the directory where makecert.exe is located)

• Once the certificate is imported, open it, and go to the “Details” tab :


Select the line “Thumbprint” and copy the associated.

• In your powershell script, edit the following line :

$cert = Get-ChildItem -Path Cert:\CurrentUser\My\AF2E902AB3DECF2BF6FB84043AF6B1B4E28A2957
Write-Host $cert

Replace “CurrentUser” by “LocalMachine” if you are rather your local computer certificate store. And then, replace the token by the thumbprint copied previously.

Execute these commands and make sure that everything is ok :

• Finally we can sign our certificate :

Set-AuthenticodeSignature -FilePath '.\UnicornRemoteSync.ps1' -Certificate $cert

• Once this is done, you just have to execute your powershell script, and enjoy your automated deployment asset ;-).

Warning : If your server responds with this kind of errors :


Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. 


Just add the following lines :

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Just before the call of the WebRequest command. It’s a workaround concerning https policy restriction, which can save you a lot of time, trust me.

III. References

Here are some of the wonderful websites I used to write down this article :
http://blog.karbyn.com/articles/sitecore-getting-started-with-unicorn/
https://github.com/kamsar/Unicorn
https://kamsar.net/index.php/2017/06/Unicorn-4-Released/
https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error

Thomas Griesmar