Sitecore Content Resolvers Guide - ysolution-it/sitecore-wiki GitHub Wiki

Overview

Content Resolvers in Sitecore are essential components that work with the Layout Service to deliver rendering data in JSON format for headless applications. They determine how content is serialized and delivered to front-end applications through the Layout Service API.

What are Content Resolvers?

Content Resolvers are server-side components that:

  • Serialize Sitecore content items into JSON format
  • Determine what data is included in the Layout Service response
  • Customize the rendering output for headless applications
  • Work alongside the Layout Service to provide structured data to JSS and other headless frameworks

Architecture Flow

Rendering => Layout Service (JSON) => Content Resolvers => Serialized Output

Built-in Content Resolvers

Sitecore provides several out-of-the-box content resolvers located at: /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers

1. Context Item Resolver

  • Purpose: Serializes the current context item
  • Use Case: When you need the current page item data
  • Output: JSON representation of the context item fields

2. Context Item Children Resolver

  • Purpose: Serializes the children of the context item
  • Use Case: For navigation menus, child page listings
  • Output: Array of child items with their field data

3. Datasource Resolver (Default)

  • Purpose: Serializes the rendering's datasource item
  • Use Case: Most common scenario - component-specific content
  • Output: JSON of the datasource item fields
  • Note: This is the default behavior when no resolver is specified

4. Datasource Item Children Resolver

  • Purpose: Serializes the children of the datasource item
  • Use Case: When datasource has child items that need to be displayed
  • Output: Array of datasource's child items

5. Folder Filter Resolver

  • Purpose: Filters and serializes items from a specific folder
  • Use Case: Content filtering based on folder structure
  • Output: Filtered list of items based on folder criteria

6. Navigation Contents Resolver

  • Purpose: Specialized resolver for navigation structures
  • Use Case: Building navigation menus and site structure
  • Output: Hierarchical navigation data

How Content Resolvers Work

1. Configuration

Content resolvers are configured in Sitecore items under:

/sitecore/system/Modules/Layout Service/Rendering Contents Resolvers

2. Assignment to Renderings

Each JSON rendering can reference a specific content resolver:

  • Navigate to your rendering item
  • Set the "Contents Resolver" field to point to your desired resolver

3. Execution Flow

  1. Layout Service receives API request
  2. Identifies the rendering and its associated content resolver
  3. Content resolver processes the item data
  4. Serialized JSON is returned to the requesting application

Creating Custom Content Resolvers

Step 1: Implement the Interface

public class CustomContentResolver : IRenderingContentsResolver
{
    public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig)
    {
        // Custom logic here
        return customObject;
    }
}

Step 2: Create Sitecore Item

  1. Navigate to /sitecore/system/Modules/Layout Service/Rendering Contents Resolvers
  2. Insert new "Rendering Contents Resolver" item
  3. Configure the Type field with your custom class

Step 3: Configure Rendering

  1. Go to your JSON rendering item
  2. Set the "Contents Resolver" field to reference your custom resolver

Best Practices

Performance Considerations

  • Minimize database calls: Cache frequently accessed data
  • Lazy loading: Only load data when needed
  • Field selection: Only serialize required fields

Security

  • Field permissions: Respect Sitecore field-level security
  • Content filtering: Filter sensitive content appropriately
  • API key validation: Ensure proper authentication

Maintainability

  • Consistent naming: Use clear, descriptive resolver names
  • Documentation: Document custom resolver logic
  • Error handling: Implement robust error handling
  • Logging: Add appropriate logging for debugging

Common Use Cases

1. Multi-level Navigation

// Custom resolver for deep navigation structures
public class MultiLevelNavigationResolver : IRenderingContentsResolver
{
    public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig)
    {
        // Logic to build multi-level navigation
    }
}

2. Filtered Content Lists

// Resolver for filtered content based on criteria
public class FilteredContentResolver : IRenderingContentsResolver
{
    public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig)
    {
        // Logic to filter content based on custom criteria
    }
}

3. Aggregated Data

// Resolver that combines data from multiple sources
public class AggregatedDataResolver : IRenderingContentsResolver
{
    public object ResolveContents(Rendering rendering, IRenderingConfiguration renderingConfig)
    {
        // Logic to aggregate data from multiple items/sources
    }
}

Configuration Options

Resolver Item Fields

  • Type: Fully qualified class name and assembly
  • Include Server URL in Media URLs: Controls media URL format
  • Use Context Item: Determines context item usage

Rendering Configuration

  • Contents Resolver: Reference to the resolver item
  • Contents Resolver Parameters: Additional parameters for the resolver

Troubleshooting

Common Issues

  1. Resolver not executing: Check Type field configuration
  2. Empty response: Verify resolver logic and data availability
  3. Performance issues: Review database queries and caching
  4. Security errors: Check item and field permissions

Debugging Tips

  • Use Sitecore logs to trace resolver execution
  • Test with Layout Service preview URLs
  • Validate JSON output format
  • Check rendering item configuration

Integration with Headless Frameworks

JSS (JavaScript Services)

Content resolvers work seamlessly with JSS applications:

  • React components receive resolved data as props
  • Angular components access data through JSS services
  • Vue.js components consume resolved content

.NET Core SDK

Content resolvers support .NET Core headless applications:

  • Model binding with resolved content
  • Strongly-typed content models
  • Integration with ASP.NET Core MVC

Conclusion

Content Resolvers are a powerful feature in Sitecore's headless architecture, providing flexible content serialization for modern web applications. Understanding how to effectively use and customize these resolvers is crucial for successful headless Sitecore implementations.

By leveraging both built-in and custom content resolvers, developers can create efficient, maintainable, and scalable headless solutions that meet specific business requirements while maintaining optimal performance and security standards.