Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaadansari/c448c057c40c4cd712c20ac369da18f4 to your computer and use it in GitHub Desktop.
Save isaadansari/c448c057c40c4cd712c20ac369da18f4 to your computer and use it in GitHub Desktop.
A custom Sitecore JSS rendering contents resolver, which serializes items into a tree-like JSON structure. More information at https://blog.vitaliitylyk.com/jss-and-arbitrary-item-hierarchies/
using Newtonsoft.Json.Linq;
using Sitecore.Data.Items;
using Sitecore.LayoutService.Configuration;
namespace VitaliiTylyk.JavascriptServices.RenderingContentsResolvers
{
/// <summary>
/// Builds a tree-like structure of datasource item's descendants
/// </summary>
public class DescendantsTreeRenderingContentsResolver : Sitecore.LayoutService.ItemRendering.ContentsResolvers.RenderingContentsResolver
{
protected override JObject ProcessItem(Item item, IRenderingConfiguration renderingConfig)
{
var jObject = base.ProcessItem(item, renderingConfig);
if (item.Children.Count == 0)
{
return jObject;
}
jObject["items"] = ProcessItems(item.Children, renderingConfig);
return jObject;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment