Using Sitecore Image field / Treelist with images in a multisite instance

To come up with a flexible solution to using shared templates across sites with image fields as well – while the sites have separate media repositories, we came up with a solution where the common Site Node (a common parent to all sites) has a field “Media Repository” where we select the corresponding media repository for each site node.


Using this setup, we are able to link a site node to its corresponding media repository and hence, point the image source of an item to its respective repository folder.

The only restriction here, is that the folder structure would need to be the same in every site media repository. But note – this will also apply to any solution we use for datasources with Sitecore queries in other link / list Sitecore fields.

The code to do this:

using Sitecore.Data.Items;
using System;
using SharedSource.Helpers;

namespace SharedSource.SitecoreFields
    public class SiteImage : Sitecore.Shell.Applications.ContentEditor.Image
        public string ItemID

        protected override void OnPreRender(EventArgs e)

            string source = ServerProperties["Source"].ToString();
            if (String.IsNullOrEmpty(ItemID) || String.IsNullOrEmpty(source) || !source.StartsWith("$sitemediapath")) return;


            Item current = Sitecore.Context.ContentDatabase.GetItem(ItemID);
            if (current == null) return;

            string siteMediaPath = GetSiteMediaPath(current);
            if (siteMediaPath == null) return;

            source = source.Replace("$sitemediapath", siteMediaPath);

        protected void SetSource(string source)
            ServerProperties["Source"] = source;
            Source = source;

        public static string GetSiteMediaPath(Item item)
            Item site = SitecoreHelper.GetAncestorOrSelfByTemplateId(item, new ID("{Site Node Template Id}"));
            Item path = SitecoreHelper.GetReferenceField(site, "Media Repository");
            return path.Paths.FullPath;

2 special custom sitecore helper methods are used here:

  • GetAncestorOrSelfByTemplateId: Basically goes recursively up the content tree to find an ancestor item match by template id.
  • GetReferenceField: This method gets the item referenced in say a droplink field, by fetching the item based on the guid stored in the field.

This content extension will need to be registered in sitecore configuration:

      <source mode="on" namespace="SharedSource.SitecoreFields" assembly="SharedSource" prefix="contentExtension" />

And we can add the extension to the simple Image field in the Core DB

Also, if you have a scenario where you’d want a content author to be able to select multiple images on an item from a treelist, then you’d want to add this support on the treelist field as well. We chose to go with a new custom field though šŸ™‚

using Sitecore.Data.Items;
using Sitecore.Shell.Applications.ContentEditor;

namespace SharedSource.SitecoreFields
    public class CustomTreeList : TreeList
        public new string Source
            get { return base.Source; }
                if (!value.StartsWith("$sitemediapath"))
                    base.Source = value;
                    Item item = Sitecore.Context.ContentDatabase.Items[this.ItemID];
                    if (item != null)
                        string source = value;
                        string mediapath = SiteImage.GetSiteMediaPath(item);
                        source=source.Replace("$sitemediapath", mediapath);
                        base.Source = source;



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