World is now on Opti ID! Learn more

Tomek Juranek
Apr 6, 2025
  0
(0 votes)

Show/hide blocks and properties based on selected site in multi-site solution

Optimizely PaaS CMS has a great support for multi-site solutions which can additionally be extended with the custom code. For example, we can enhance it to show or hide entire blocks or blocks elements based on the selected site in a multi-site setup. This enables tailored content experiences across different domains while maintaining a single content structure.

First let's create enum with list of our sites, it should match site definition names in CMS Settings -> Manage Websites panel:

public enum MySite 
{
    Site1,
    Site2,
    Site3
}

We will also need a helper class to resolve a site based on current host name and port (helpful for local development):

public interface ISiteHelper
{
    public MySite GetCurrentSite(ExtendedMetadata metadata);
}

public class SiteHelper : ISiteHelper
{
    private readonly ISiteDefinitionResolver _siteDefinitionResolver;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public SiteHelper(ISiteDefinitionResolver siteDefinitionResolver, IHttpContextAccessor httpContextAccessor)
    {
        _siteDefinitionResolver = siteDefinitionResolver;
        _httpContextAccessor = httpContextAccessor;
    }

    public MySite GetCurrentSite(ExtendedMetadata metadata)
    {
        dynamic contentMetadata = metadata;
        var ownerContent = contentMetadata?.OwnerContent as IContent;

        SiteDefinition siteDefinition = null;
        if (ownerContent != null && ownerContent?.ContentLink?.ID != 0)
        {
            siteDefinition = _siteDefinitionResolver.GetByContent(ownerContent?.ContentLink, true);
        }

        if (siteDefinition == null){
            var request = _httpContextAccessor.HttpContext?.Request;
            var host = request?.Host.Host;
            if (request.Host.Port != null)
            {
                host += ":" + request.Host.Port;
            }
            siteDefinition = _siteDefinitionResolver.GetByHostname(host, true);
        }

        if (siteDefinition != null)
        {
            foreach (var name in Enum.GetNames(typeof(MySite)))
           {
               if (siteDefinition.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
               {
                   return (MySite)Enum.Parse(typeof(MySite), name);
               }
           }
        }
        throw new Exception($"Site not configured");
    }
}
 

Next we can implement an attribute which utilizes the helper class described above. Besides hiding the element, we want to ensure that if property is required but should be hidden validation logic won't be execured for that site:

public class ShowOnSitesAttribute : Attribute, IDisplayMetadataProvider
{
    private readonly MySite[] _sites;
    private readonly ISiteHelper _siteHelper;
    public bool Hide => !_sites.Contains(_siteHelper.GetCurrentSite());
    
    public ShowOnSitesAttribute(params MySite[] sites)
    {
        _sites = sites;
        _siteHelper = ServiceLocator.Current.GetService();
    }

    public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
    {
         var extendedMetadata = context.DisplayMetadata.AdditionalValues[ExtendedMetadata.ExtendedMetadataDisplayKey] as ExtendedMetadata;
         if (_sites.Contains(_siteHelper.GetCurrentSite(extendedMetadata)))
        {
            return;
        }

        if (extendedMetadata == null)
        {
            return;
        }

        extendedMetadata.ShowForEdit = false;
        foreach (var property in extendedMetadata.Properties)
        {
            property.IsRequired = false;
            property.ValidationMetadata.IsRequired = false;
        }
    }
} 

Finally we can use the attribute in a block for entire class or for the properties:

[ContentType(DisplayName = "My Block", GUID = "A4ED27A1-2B34-4323-885E-4B478A137578")]
[ShowOnSites(MySite.Site1, MySite.Site2)]
public class MyBlock : BlockData
{
    [CultureSpecific]
    [Required]
    [Display(Name = "Heading", GroupName = TabNames.Content, Order = 10)]
    [ShowOnSites(MySite.Site1)]
    public virtual string Heading { get; set; }

    [CultureSpecific]
    [Display(Name = "Description", GroupName = TabNames.Content, Order = 20)]
    public virtual XhtmlString Description { get; set; }
} 
Apr 06, 2025

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |