Adding dynamic image source for multi-site solutions with $site$ token and setting dynamic datasource location for components

Months after finishing a project I have had clients saying:
We need to make a copy of the site you created and put some other content on it
Most clients need to put this new site into the same solution because of restrictions in their Sitecore license.
To convert old projects there was usually a lot of content restructuring and code changes needed.
I have done this exercise multiple times.
I try to have this in mind when creating new solutions so we don`t end up with too much effort when adding support for multi sites at a later stage.

Even with great structure and code we still face some problems.
In a multi-site solution you want to reuse components, but the sites should not always share the data locations and image folders.
Most sites have different editors, they should not see the components and images from the other sites, we then need to place content in different folders and set the appropriate security on those.

I recently got a request to duplicate an existing site, they wanted to use the same design and components with minor variations. My initial though, was to introduce one of the many modules for local data sources. There was a lot of content on the existing site, there was not enough time to convert this to local data sources before going live with the new site. They also had a lot of shared components in use for the initial site.

What I did to create a multi-site solution from the existing solution 

The initial site was not planned to be a multi-site solution, So I had to go over the initial site and analyze it. I did some restructuring and code changes, I choose to not cover all of the steps in this blog post, but might create a new one with my thoughts on what to look for when converting a non multi-site solution to a multi-site solution.

The initial structure was a Home node right after the Content node with its settings folder and global modules folder.
I created a new template SiteRoot and an item based on this.
I moved the existing content to this item and duplicated the needed folders for the second site.

After the duplication the two Sites had this structure:


And the initial site had this media folder structure(I created the same structure for the new site):

The problems

I identified two problems I wanted to fix:
1. I needed to tell Sitecore where to create the component items based on the current site the editor is working on.
2. I needed to know where in the media library the Image should be uploaded based on the Site the editor is working on.

Solution how set dynamic datasource location for components

I solved this by adding a query to the Datasource Location on the renderings:
  • The first template ID is for the common siteroot.
  • The second template ID is for the component folder
The Microsite Data can also be replaced by a template ID, I kept this since it will be the same for all components in this solution.
This works with content editor and experience editor.

Solution how to add dynamic image source

There are several solutions to create dynamic image sources, none of those I found was flexible enough and I wanted to have a solution with a site token to use in other projects.

I started by adding /$site$/FullWidthImage to the Image Source.

I needed a way to replace the site token with the correct path.
I created a new field on my Site Root item MediaFolderRoot.
In this field we add the name of the media folder that we want to replace the token with.

I would like to change this field to a selector instead of a string. We could then get the name of the selected media folder item instead of using a string. I might update this at a later point.

Helpers
In my helpers I have two methods. Get the correct root item and get the string that we would want to replace the token with.
CustomImageField.cs 
I check if the type is Image and if the field source starts with the token. This way we wont break the other fields or existing images in the solution.
Changes to Core database
/sitecore/system/Field types/Simple Types/Image
Adding Assembly, Class and Control
contentExtension is the prefix added under the controlSources setting.

Changes to controlSources setting

Fix for Experience editor
This works in content editor but as Sitecore John writes in his blog post we need to override the webedit:chooseimage command. I collected the updated code from Sitecore\Sitecore.ExperienceEditor.dll.
I recommend to not copy paste this code but check your current version of Sitecore and change the code to make it work.
The code I changed has the start modification and end modification comments

Register the command

Additional info

It is possible to create a new simple type field MutliSiteImage instead of using the standard Image.
This would introduce the need for extra logic to publish related items and other issues not covered by this blog post.

Comments

Popular posts from this blog

Step by step guide to add Glass Mapper to Helix solution

Using interfaces with Glass Mapper

Enable PowerPaste to Sitecore Rich Text Editor