Sitecore content dependencies options for Partial Html Cache

Created: 22 Sep 2021, last update: 30 Jan 2022

Sitecore 10.1 Build a Home Item Content dependencies option for Partial Html Cache

When using the Partial Html Rendering Cache you can specify the Content Dependencies, When the rendering is rendered, it is placed in the cache and the content decencies are gathered and stored linked to the cached item. Out of the box Sitecore offers content dependencies for Ancestors, Parents, Siblings, Related Items, Recursive Related Items, Children and Descendants.

When you store some general config in the Home Item or Site Item and you use this in a MVC Rendering. You can use the “Ancestors” option but this does not fit always because the datasource may be outside the website and also it adds more dependencies than you need. So I created an option that uses the Sitecore.Context to find out the right website.

Build a Siteroot and Home Item Content Dependencies option
Add option to /sitecore/system/Settings/Layouts/Renderings/Content Dependency Types
For the new types just add an item with template
/sitecore/templates/System/Item

You only need to know the itemId. Use the Item id to check in the pipeline processor if it is in the RenderingItem.ContentDependencies.Contains

The code of the pipeline processor for Siteroot:

using Sitecore.Data.Items;
using Sitecore.Layouts.Models;
using Sitecore.Pipelines.GetRenderingContentDependencies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContentDependencies
{
    public class SiteRoot
    {
        //see https://doc.sitecore.com/en/developers/101/sitecore-experience-manager/configure-html-caching.html#UUID-bf630a57-97ea-900b-6d62-ad57c7fe83a5_section-idm232136386794561
        public void Process(GetRenderingContentDependenciesArgs args)
        {
            if (args.Datasource == null || args.Datasource.Count == 0 || args.RenderingItem == null)
            {
                return;
            }

            if (args.RenderingItem.ContentDependencies.Contains(new Sitecore.Data.ID("{7DE7D100-0B37-46AA-AE56-01120FC1EE0F}")))
            {
                var result = new List();

                Item rootitem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.RootPath);
                ContentDependencyEntry entry = new ContentDependencyEntry(rootitem);
                result.Add(entry);

                args.AddResolvedDependencies(result); // submit the result
            }
        }
    }
}

And the c# code for Home Item:

using Sitecore.Data.Items;
using Sitecore.Layouts.Models;
using Sitecore.Pipelines.GetRenderingContentDependencies;
using System.Collections.Generic;

namespace ContentDependencies
{
    public class HomeItem
    {
        //see https://doc.sitecore.com/en/developers/101/sitecore-experience-manager/configure-html-caching.html#UUID-bf630a57-97ea-900b-6d62-ad57c7fe83a5_section-idm232136386794561
        public void Process(GetRenderingContentDependenciesArgs args)
        {
            if (args.Datasource == null || args.Datasource.Count == 0 || args.RenderingItem == null)
            {
                return;
            }

            if (args.RenderingItem.ContentDependencies.Contains(new Sitecore.Data.ID("{F717D198-FAC9-45E7-88AF-E48FF11494A0}")))
            {
                var result = new List();

                Item hometitem = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
                ContentDependencyEntry entry = new ContentDependencyEntry(hometitem);
                result.Add(entry);

                args.AddResolvedDependencies(result); // submit the result
            }
        }
    }
}

Add the pipeline processor to the getRenderingContentDependencies place .config file below /App_Config/Include/

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getRenderingContentDependencies>
        <processor type="ContentDependencies.SiteRoot, ContentDependencies"/>
        <processor type="ContentDependencies.HomeItem, ContentDependencies"/>
      </getRenderingContentDependencies>
    </pipelines>
  </sitecore>
</configuration>

And use the new Content dependencies options in your rendering

See GitHub for Visual Studio Solution: https://github.com/jbluemink/ContentDependencies

See Sitecore Doc site for general info configure-content-dependencies-for-an-mvc-rendering

See Sitecore Partial HTML Cache explained and Developer tool