We looked thru Contents Resolvers in previous post, so in this one let’s check out how to create your own one. This may be helpful when your rendering requires some additional data, that can’t be retrieved with standard resolvers.
As you might notice - there are no content resolver which includes parent item as well as children - it’s always a one thing. So, let’s implement this functionality as it seems like common option to store some settings in parent items and specific data in children items.
Class Implementation
To implement your own content resolver you have to create resolver class that inherits Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver class and overrides it’s ResolveContent method:
using Sitecore.Diagnostics;
using Sitecore.LayoutService.Configuration;
using System.Collections.Generic;
namespace Platform.Processors
{
public class DatasourceWithChildrenContentsResolver : Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver
{
public override object ResolveContents(Sitecore.Mvc.Presentation.Rendering rendering, IRenderingConfiguration renderingConfig)
{
// Ensure that needed data passed
Assert.ArgumentNotNull(rendering, nameof(rendering));
Assert.ArgumentNotNull(renderingConfig, nameof(renderingConfig));
// Get context item
var contextItem = GetContextItem(rendering, renderingConfig);
if (contextItem == null)
{
return null;
}
// Serialize context item into JSON
var jObject = ProcessItem(contextItem, rendering, renderingConfig);
// Get child items list
var childItems = new List<Sitecore.Data.Items.Item>();
childItems.AddRange(GetItems(contextItem) ?? new List<Sitecore.Data.Items.Item>());
// Serialize child items to JSON's field "items"
jObject["items"] = ProcessItems(childItems, rendering, renderingConfig);
return jObject;
}
}
}
As you might notice, most of methods that operates with single item or items collection already implemented in base class and we just composing resulting JSON object based on data retrieved from this methods.
Adding resolver’s Content Item
To make Sitecore know about our Contents Resolver we have to create special item under /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers folder. It’s recommended to create custom implementations under subfolder(s) just to separate them from originals. So, under this folder let’s create Rendering Contents Resolvers Folder item and name it “Custom” and create Rendering Contents Resolver inside naming it “Datasource Item With Children Resolver”. There are couple of fields that we have to fill:
-
Type: Put the namespace of the custom resolver class and its assembly, for my example it will be like so: Platform.Processors.DatasourceWithChildrenItemResolver, Sitecore.Platform
-
Include Server URL in Media URLs: checked by default
-
Use Context Item: this field is unchecked, by default, which means that the resolver will use the datasource item(of the rendering) rather than the Context Item. When it is selected, the JSON object will include route level data. For our example, we will keep it unchecked.
-
Item Query Selector: This field allows the user to add queries which can manipulate how the content is being processed. For this example let’s use “/*” query which will process all items.
That’s it!
And that’s it. Now, you can go to desired rendering item and select your new contents resolver that will be in a list from the moment of save.