Configure content dependencies for an MVC rendering

Abstract

Shows how you specify content dependencies for an MVC rendering

You associate renderings with content by using the data source property (or the page item, when you do not specify a data source). You can access the content in your controller or view code by using the Item or Items properties of the RenderingContext.Current.Rendering object.

However, there are scenarios where your rendering design requires that you go beyond the item (or items) and navigate through associated or related items. For example, you can use the data source as a folder or container of your actual content. In this case, you need to set the data source property to the folder item and use the RenderingContext.Current.Rendering.Item.Children object to access the content in your controller or view code.

Another example is the breadcrumb rendering. You can use it to navigate through the ancestors of the current page item. In general, the rendering might expand the data source item (or items) to some of its dependencies. These dependencies can be categorized as lineage or related dependencies.

You can specify these content dependencies for a rendering item in Sitecore. This is important if you use features such as partial HTML cache clearing and page content search.

To configure rendering content dependencies:

  1. Navigate to the rendering definition item in the content tree.

  2. Select one or more options in the Content Dependencies field:

    Content Dependencies field

    The options are:

    • Ancestors

      The rendering uses ancestors of the data source item or items.

    • Parent

      The rendering uses the parent of the data source item or items.

    • Siblings

      The rendering uses siblings of the data source item or items.

    • Related Items

      The rendering uses related items of the data source item or items.

    • Recursive Related Items

      When you use this option with other options (except Related Items), it indicates that related items of that option's items are also used by the rendering. For example, if you use this option with the Ancestors option, it indicates that all ancestors along with their related items are used by the rendering. This option is designed specifically to work with other lineage options. If you use it on its own, it works the same way as Related Items. In such cases, use Related Items instead.

    • Children

      The rendering uses direct children of the data source item or items.

    • Descendants

      The rendering uses descendants of the data source item or items.

If the default content dependencies options do not fit your needs, you can create your own.

To create an option:

  1. In the content tree, create an option item here: /sitecore/system/Settings/Layouts/Renderings/Content Dependency Types, based on the Item template (sitecore/templates/System/Item).

  2. Extend the getRenderingContentDependencies pipeline by adding a processor that inspects the option and adds the relevant dependencies into the pipeline result. A typical processor implementation looks like this:

     public void Process(GetRenderingContentDependenciesArgs args)
    {   
        if (args.Datasource == null || args.Datasource.Count == 0)
        {
            return;
        }
    
        if (args.RenderingItem?.ContentDependencies.Contains("THE OPTIONS ITEM ID HERE"))
        {
            var result = new List<ContentDependencyEntry>();
    
            foreach(var datasource in args.Datasource)
            {
                // populate the result with the dependencies 
            }
            
            args.AddResolvedDependencies(result); // submit the result
        }
    }