Sitecore Rendering Host: How to access rendering parameters

Sitecore provides a clear separation between the data which will be displayed and details of how it will be displayed. Rendering Parameter is a way to pass the additional data to the component intended to control how it should be rendered, while data source defines data itself.

Working with MVC applications, there is no difficulties to reach Rendering Parameters because they are always available in rendering/component context. But, in case of headless architecture and in ASP.NET Core Rendering Host specifically, things go different. Sitecore uses the Layout Service to pass all serialized data to the “head” including rendering parameters. If we have a look at the component output json, we will see the following:

{
    "uid": "0a563e29-1d02-44b1-b84c-db0ffe77c16b",
    "componentName": "BlogOverview",
    "dataSource": "/sitecore/content/Global/Data/Blog Overview",
    "params": {
        "ShowAllBlogs": "1"
    },
    "fields": {
        "SelectedTags": [],
        "Headline": {
            "value": "Blog"
        }
}

where the params object represents rendering parameters associated with the component. Now we need to map the properties to the view model. Lookin at the following documentation https://doc.sitecore.com/en/developers/101/developer-tools/model-binding.html, it seems obvious and easy - we need to use [SitecoreComponentProperty(Name = "componentproperty1")] to access component properties. When  I have extended the view model property with corresponding attribute, I got the following:

public class BlogPostOverview
{
public TextField Headline { get; set; }

public ContentListField<Tag> SelectedTags { get; set; }

[SitecoreComponentProperty(Name = "ShowAllBlogs")]
public string ShowAllBlogs { get; set; }
}

But it does NOT work! ShowAllBlogs property is null.

Looking deeper to the SitecoreComponentProperty code, I found out that it just accesses the properties of Sitecore.LayoutService.Client.Response.Model.Component object. You can find the list of available properties below:

  • Id: string (which equal to uid property of json object)

  • Name: string (which equal to componentName property of json object)

  • DataSource: string (which equal to dataSource property of json object)

  • Parameters: Dictionary<string, string> (which equal to params property of json object)

  • Placeholders: Dictionary<string, Placeholder> (which equal to Placeholders property of json object)

Only properties above are available to be used within SitecoreComponentProperty attribute. And my final model look as following:

public class BlogPostOverview
{
public TextField Headline { get; set; }

public ContentListField<Tag> SelectedTags { get; set; }

[SitecoreComponentProperty(Name = "Parameters")]
public Dictionary<string, string> Parameters { get; set; }

public string ShowAllBlogs => Parameters.ContainsKey("ShowAllBlogs") ? Parameters.GetValueOrDefault("ShowAllBlogs") : String.Empty;
}

Where ShowAllBlogs contains a value from the component rendering parameters.