In a Sitecore headless implementation, the Layout Service typically returns the fields associated with a rendering's datasource.
However, not every component has straightforward content requirements.
Consider a Product Listing component where the datasource is a folder containing multiple product items. Returning only the fields of the datasource folder would not satisfy the component's requirements.
This is where Rendering Contents Resolvers become useful.
What is a Rendering Contents Resolver?
A Rendering Contents Resolver is a server-side extension point in Sitecore Headless Services that determines what component data is included in the Layout Service response.
It allows developers to customize the data contract between Sitecore and the frontend application without necessarily introducing a separate API.
Common use cases include:
Navigation and breadcrumb components.
Product listings.
Related articles.
Content aggregations.
Computed business data.
Custom component-specific content structures.
Built-in Rendering Contents Resolvers
Sitecore provides several resolvers that address common content retrieval requirements.
Datasource Resolver
Returns the fields associated with the rendering's datasource item. This is the default behavior.
Datasource Item Children Resolver
Returns the child items of the configured datasource. Useful for components such as card collections and promotional listings.
Context Item Resolver
Retrieves content from the current page instead of the rendering datasource.
Context Item Children Resolver
Retrieves child items associated with the current page.
Folder Filter Resolver
Retrieves descendants from the datasource while excluding folder items.
Before developing a custom resolver, evaluate whether an existing resolver already satisfies the requirements.
Building a custom Rendering Contents Resolver
Consider a Product Listing component that needs to display a maximum of six published product items, including their editable content fields.
We can implement this requirement by extending the RenderingContentsResolver class.
using System.Linq;
using Sitecore.LayoutService.Configuration;
using Sitecore.LayoutService.ItemRendering.ContentsResolvers;
using Sitecore.Mvc.Presentation;
namespace Feature.Products.Resolvers
{
public class ProductListingResolver
: RenderingContentsResolver
{
public override object ResolveContents(
Rendering rendering,
IRenderingConfiguration renderingConfig)
{
var datasource = GetContextItem(
rendering,
renderingConfig);
if (datasource == null)
{
return new
{
items = new object[0]
};
}
var products = datasource.Children
.Where(item =>
item.Access.CanRead() &&
item.Versions.Count > 0)
.Take(6)
.ToList();
return new
{
items = products.Select(item =>
ProcessItem(
item,
rendering,
renderingConfig))
.ToArray()
};
}
}
}The resolver retrieves a limited collection of child items and uses Sitecore's existing field serialization mechanism.
Using ProcessItem instead of manually returning raw field values helps preserve the field serialization required for Sitecore editing capabilities.
In a production implementation, I would also filter items by the relevant product template ID.
Configuring the resolver in Sitecore
Navigate to:
/sitecore/system/Modules/Layout Service/Rendering Contents Resolvers
Create a new Rendering Contents Resolver item named Product Listing Resolver.
Configure its Type field:
Feature.Products.Resolvers.ProductListingResolver,
Feature.ProductsKeep Use Context Item unchecked when the resolver should operate on the rendering datasource.
Next, navigate to the Product Listing JSON Rendering definition and select the newly created resolver in its Rendering Contents Resolver field.
Deploy the assembly to the relevant Sitecore CM and CD instances and publish the necessary configuration items.
Understanding the Layout Service response
After configuring the resolver, the component's response could look like this:
{
"componentName": "ProductListing",
"dataSource": "{DATASOURCE-ID}",
"fields": {
"items": [
{
"Title": {
"value": "Product A"
}
},
{
"Title": {
"value": "Product B"
}
}
]
}
}The frontend component can consume this collection directly without making another request to retrieve the product items.
Rendering Contents Resolver vs GraphQL
A custom resolver is not always the best solution.
For straightforward content relationships, a built-in resolver may be sufficient.
Integrated GraphQL can be useful when the component requires specific fields and related content that can be expressed through a query.
A custom C# resolver becomes relevant when the requirement involves specialized server-side processing that cannot be handled easily through configuration or GraphQL.
Connected GraphQL may be more suitable for interactive filtering, pagination and independently loaded content.
The decision should be driven by the component requirements rather than individual technology preferences.
A practical production scenario
Consider a homepage containing twelve components, each using a custom Rendering Contents Resolver.
The Layout Service response gradually becomes slower as the content volume increases.
After investigation, we discover that several resolvers traverse large content trees and perform repeated database operations.
Adding more Content Delivery servers might temporarily improve throughput, but it does not eliminate the underlying inefficiency.
I would review:
Content retrieval and traversal operations.
Maximum number of returned items.
Opportunities to use ContentSearch and Solr.
Rendering output caching.
External API dependencies.
JSON payload size.
Individual resolver execution time.
A resolver executes within the backend content delivery process. Expensive resolver operations can directly affect page-data retrieval performance.
Architect's takeaway
Rendering Contents Resolvers are a powerful extension point, but they should not become a dumping ground for application business logic.
Prefer built-in capabilities where possible. Introduce custom C# implementations only when the requirements justify the additional maintenance and deployment responsibilities.
A good resolver returns predictable, limited and well-structured data while preserving the editorial experience.
In headless architecture, the quality of the component's data contract is just as important as the quality of its frontend implementation.


No comments:
Post a Comment