Creating Preview / Draft Sites in SXA using SPE

sorcerers-stone-banner

A few weeks ago I was tasked with providing the ability for content editors to be able to have a preview (or draft) site for SXA sites when using the CM Url. The requirement was to be able to view ‘Draft’ content/versions of pages (without publishing them) for stakeholders to be able to preview them before they are published to the CD instance.

In an Traditional Sitecore setup this is pretty easy, you would just edit the site definition config in the Sitecore config with a patch file to change the database used to Master when on the CM.
For SXA this is a bit trickier as the Site definitions are dynamically built by SXAs site manager when Sitecore starts up (or the SXA Site Manager is ran again in Sitecore).

However after reading around it seems it was possible to achieve what I needed to do. I had to duplicate the existing Site Grouping definition item and change the database that it uses to Master. See an example below:

draft-site-def

I also changed the environment to ‘Draft’ and added the following Patch file on the CM only, this tells the CM that the current environment is ‘Draft’ and to use Site Definition items with that environment setting:

 <sitecore>
    <settings>
       <!-- Environment name used by cm-->
       <setting name="XA.Foundation.Multisite.Environment" value="Draft" />
    </settings>
 </sitecore>

The last thing I did is change the name of the Site Grouping definition item and the title to include the word ‘Draft’. This was to make it clearer to content editors and stakeholders that they are editing/viewing ‘Draft’ content.

This results in each SXA site having two site Site Grouping definition items; the original (pointed at the Web DB) which the CD instance will use and the new Preview/Draft site that the CM instance uses:

draft-site-structure

Automating Creating of Draft Sites with SPE

As I needed to create Draft sites for quite a few sites I wrote an SPE script to do this for me:

 

Deleting Draft Sites

Should you need to remove your draft Sites (e.g during local testing) then I wrote a script for that too:

Site Ordering

One thing I discovered is that the order of the Sites in the SXA site manager matters. SXA will return the first one it matches based on Environment and the order defined here:
/sitecore/system/Settings/Foundation/Experience Accelerator/Multisite/Management/Sites

You therefore need to ensure the Draft site is above the original site grouping in the list. You can re-order these in the SXA Site manager using the up and down arrows but I had some issues with this not taking affect so instead I moved the draft sites up the list in the field they are stored in instead. As I had a lot of sites to do this for I actually ended up writing an SPE script to move all the draft sites to the top of the list:

 

Right-Click ‘Create Draft Site’

The above scripts were really useful to get the draft sites setup for the existing sites but I also needed a way to quickly add draft sites for new SXA sites in future. I decided an nice way or doing that would be using an SPE context menu script.

right-click-create-draft-site

Here is the script, make sure you place it here: e.g: /sitecore/system/Modules/PowerShell/Script Library/{YOUR SCRIPT LIBRARY}/Content Editor/Context Menu:

Get CM Url Function:

Move Draft Site Function:

Experience Editor Notification

draft-exp-editor-message

Again in order to make it clearer to content editors and stakeholders that they are editing/viewing ‘Draft’ content I decided an Experience Editor Notification would be useful. So again I turned to SPE to do this. I’m not sure if that many people know you can do this with SPE but it’s a really simple way to it with no C# code or config required:

Note that if you are using SPE 5 on Sitecore 9.02 or 9.01 then you will likely find there is a bug in the SPE config and you need to patch it like so to move the getPageEditorNotifications to the correct section of the config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
 <pipelines>
 <group name="ExperienceEditor" groupName="ExperienceEditor">
 <pipelines>
 <!--patch to add SPE 5.0 PageEditorNotifications in correct config group-->
 <getPageEditorNotifications>
 <processor type="Cognifide.PowerShell.VersionSpecific.Integrations.Pipelines.PageEditorNotificationScript, Cognifide.PowerShell.VersionSpecific"/>
 </getPageEditorNotifications>
 </pipelines>
 </group>
 </pipelines>
 </sitecore>
</configuration>

The end result of this work is that I now had Draft site functionality setup for all required sites, quick ways to add new draft sites as needed and it is clear to content editors that the draft site content is in use on the CM.

Dedicated Preview Instance considerations

Obviously for some scenarios you might have wanted to have a dedicated Preview/Draft instance with an specific url such as www.preview-mysite.com and publish to an ‘Preview’ database publication target. The config for the environment and hostname would then be used here instead. However for our purposes using the CM url is fine as the access is restricted and cuts down on costs of an additional instance in Azure.

Useful Links

As usual there were some really useful blog posts which helped point me in the right direction with this and figure out what I needed to do:

https://ggullentops.blogspot.com/2019/04/publishing-target-in-sitecore-sxa.html

https://getfishtank.ca/blog/setting-up-multiple-hosts-sitecore-sxa-site

https://blog.senktas.net/2019/10/20/sitecore-powershell-extensions-integration-with-experience-editor/

 

Hopefully others will find this information and the scripts I’ve written for setting up Preview sites in SXA useful in future.

p.s – if you wondering why the Harry Potter image; well apparently it took J. K. Rowling 13 attempts to finally get someone to publish the Philosopher’s Stone so it nearly remained in Draft and never published!

Published by

Adam Seabridge

I am a Freelance Sitecore Developer and 7x Sitecore Technology MVP (2024 - 2018). I have been working with Sitecore since 2013. You can reach me on twitter @billyjava or comment below if you have any questions or feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *