2

I'm working on a site that is on Sitecore 10 but still uses webforms for the main layout and sublayouts. I'm trying to figure out if it is possible to add a placeholder to the footer where the authors can add or remove sublayouts, and have this content display on EVERY page. In MVC, I would use the Cascade checkbox on the renderings to propagate them to all child pages, but the webforms sublayouts do not have this option. Is this even possible in webforms?

4

1 Answer 1

2

Here is an article explaining how to do it https://laubplusco.net/inheriting-renderings-sitecore/

I am briefing the process here, you can find more details with code in articles itself.

  • Add a placeholder in a rendering/sublayout where you wish to inherit your rendering
  • Add this placeholder key and rendering id in a Constant class for further usage
  • Now, you will need to add a processor extending insertRenderings pipeline.
public class AddFallbackRenderings : InsertRenderingsProcessor
{
    public override void Process(InsertRenderingsArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        if (!args.HasRenderings || args.ContextItem == null || Context.Site.DisplayMode == DisplayMode.Edit || InheritRenderingsService.SkipInherit(args.ContextItem))
            return;

        using (new ProfileSection("Inserting inherited renderings"))
        {
            if (!args.Renderings.Any(r => r.Placeholder.EndsWith(GetPlaceHolderKey(Constants.Keys.AsideSpotsKey), StringComparison.InvariantCultureIgnoreCase)))
                InheritRenderingsService.Inherit(args.ContextItem.Parent, args.Renderings, new ID(Constants.Renderings.AsideSpotsHolderId), Constants.Keys.AsideSpotsKey);
        }
    }

    protected virtual string GetPlaceHolderKey(string splotplaceholderKey)
    {
        return StringUtil.EnsurePrefix('/', splotplaceholderKey);
    }
}

This is a helper class from above article that contains main logic -

internal class InheritRenderingsService
{
    internal static void Inherit(Item inheritItem, List<RenderingReference> targetRenderings, ID renderingId, string placeholderKey)
    {
        if (inheritItem == null || SkipInherit(inheritItem) || inheritItem.Paths.FullPath.Equals(Context.Site.StartPath, StringComparison.InvariantCultureIgnoreCase))
            return;

        var renderings = inheritItem.Visualization.GetRenderings(Context.Device, true);
        if (!renderings.Any(r => r.RenderingID.Equals(renderingId)))
            return;
    
        var renderingsToInherit = renderings.Where(r => r.Placeholder.EndsWith(placeholderKey, StringComparison.InvariantCultureIgnoreCase)
    || r.Placeholder.ToLowerInvariant().Contains(string.Concat(placeholderKey, "/").ToLowerInvariant())).ToArray();
        if (!renderingsToInherit.Any())
            Inherit(inheritItem.Parent, targetRenderings, renderingId, placeholderKey);
        else
        InsertRenderings(renderingsToInherit, targetRenderings);
    }

    private static void InsertRenderings(RenderingReference[] renderingsToInherit, List<RenderingReference> targetRenderings)
    {
        foreach (var renderingReference in renderingsToInherit)
        {
            renderingReference.Placeholder = CleanPlaceholderName(renderingReference.Placeholder);
        }
        targetRenderings.InsertRange(targetRenderings.Count, renderingsToInherit);
    }

    private static string CleanPlaceholderName(string placeholderKey)
    {
        var placeholderKeys = placeholderKey.Split('/');
        return placeholderKeys[placeholderKeys.Length - 1];
    }

    internal static bool SkipInherit(Item item)
    {
        var checkBox = new CheckboxField(item.Fields[Constants.Fields.StopSpotInheritance]);
        return checkBox == null || checkBox.Checked;
    }
}
  • You can also add a checkbox at Page level to decide where to display inherited rendering (field : StopInheritance)
  • Now make sure to patch above processor just before Sitecore.Pipelines.InsertRenderings.Processors.EvaluateConditions
< processor type="[NAMESPACE].AddFallbackRenderings, [ASSEMBLY]" />

Also, you can checkout this article from Akshay Sura with similar implementation especially for multisite implementation.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.