In this article, let us see how we will be a creating a simple AuthorGrid component in Sitecore 10. Till the introduction of Sitecore10, creating a component in Sitecore mostly will be a type of View rendering or a Controller rendering component that follows .NET MVC.
I have created a sitecore site that resembles the SUGCON2022 official website https://europe.sugcon.events/ for demo purpose.
Why do we need a Custom Content Resolvers
In some scénarios the data will not be coming from the rendering data sources or the context items. So we need to somehow return the data to the LayoutService
We also need to process the datasources in custom manner to feed the datasources to the current rendering. In these scenarios the Custom Content Resolvers will come into play.
Basic step create a Custom Content Resolvers
CustomContentResolver class has to be created
Need to assign this to the Rendering Contents Resolver in Sitecore
And then this rendering contents resolver will be mapped in the JsonRendering
The custom content resolvers must be inherited from the parent class RenderingContentsResolver.
Let us see how to create Custom Components with CustomContentResolver in detail
My local environment is having two sites. By default the asp.net rendering host will not be supporting multisites. In order to achieve this you need to tweak a bit which you can refer from a very good post from Ram.
Creating the template
I have created below templates with basic fields like AuthorName, DisplayPicture etc., with respective datasources.
Creating a Custom Rendering Contents Resolver
For the Headless development with dotnet core, one of the main piller is the SitecoreLayoutService. SitecoreLayoutService is the key that serves the layout details of the page in JSON format that can be consumed by the rendering host. You can find the more details about it from my previous post.
So here our content structure will be looking like below. As its a AuthorGrid, we need to resolve the content manually using the custom content resolvers and respond them as part of the layout response.
Let us create a new project like below for the custom content resolver. The structure looks like below,
Creating the Model based on the template
I have created two models Author and Authors for our scenario. Authors.cs is the class that contains List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The below set of code is responsible of generating the authorlist which will be part of the layout response. It will read the children items from the datasource of the rendering and it returns the author list. If you look at the below code, it will be easily understandable. We have simply created some service classes and register them as a service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creating the Rendering Content Resolversand the Json Rendering
Above we have seen how to created the custom content resolver code and we have to add them in the sitecore as below with correct assembly.
The custom content resolvers to be registered in the path “/sitecore/system/Modules/Layout Service/Rendering Contents Resolvers/”
Json Rendering is as below and have to map the Rendering Contents Resolver in the Layout service section.
Once this is done and if we look at the json response of the layout service of a page item which is having this particular component, we can notice that the LayoutService response data will be having the data which gets resolved by our CustomContentResolver like below.
Creation of the Rendering Host project for the Authors Grid Component
To the Mvp solution, we will be creating a new project for this AuthorsGrid component as Mvp.Feature.Authors.Rendering where we will be registering the view and the model.
The follwing model will be created for this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Registering the ViewComponent. We will be using the .AddModelBoundView() method to register this view component. Please make sure the component name is same as what you have given the Rendering item. In this case its “AuthorsGrid” and along with the model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters