Skip to content

Instantly share code, notes, and snippets.

@parrya
parrya / ContentsResolver2.cs
Created October 22, 2021 03:58
Rendering contents resolver version 2
using Newtonsoft.Json.Linq;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.LayoutService.Configuration;
using System.Collections.Generic;
using System.Linq;
namespace JSSDemo.Feature.Blog.Extensions
@parrya
parrya / ContentsResolver1.cs
Created October 22, 2021 03:57
Rendering contents resolver version 1
using Newtonsoft.Json.Linq;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.LayoutService.Configuration;
using System.Collections.Generic;
using System.Linq;
namespace JSSDemo.Feature.Blog.Extensions
{
@parrya
parrya / LayoutServicePatch.config
Created October 22, 2021 03:53
Patch layout service to extend Item Context
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<group groupName="layoutService">
<pipelines>
<getLayoutServiceContext>
<processor type="JSSDemo.Feature.Metadata.PipelineProcessors.ItemContext, JSSDemo.Feature.Metadata" />
</getLayoutServiceContext>
</pipelines>
</group>
using Sitecore.Diagnostics;
using Sitecore.LayoutService.ItemRendering.Pipelines.GetLayoutServiceContext;
using System.Collections.Generic;
using Sitecore;
using Sitecore.Links;
namespace JSSDemo.Feature.Metadata.PipelineProcessors
{
public class ItemContext : IGetLayoutServiceContextProcessor
{
@parrya
parrya / LayoutServicePatch.config
Last active October 22, 2021 03:46
Patch layout service renderJsonRendering
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<group groupName="layoutService">
<!-- NOTE: This patch will be have a slightly different signature in Headless Services for Sitecore 9.x vs 10. The below works for Headless Services in Sitecore 10 -->
<pipelines>
<renderJsonRendering>
<processor type="JSSDemo.Foundation.LayoutService.PipelineProcessors.CustomInitialize, JSSDemo.Foundation.LayoutService" resolve="true"
patch:instead="*[@type='Sitecore.LayoutService.Presentation.Pipelines.RenderJsonRendering.Initialize, Sitecore.LayoutService']"/>
</renderJsonRendering>
@parrya
parrya / CustomInitialize.cs
Created October 22, 2021 03:43
Override Initialize method of RenderJsonRendering
using System.Collections.Generic;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.LayoutService.Configuration;
using Sitecore.LayoutService.ItemRendering;
using Sitecore.LayoutService.Presentation.Pipelines.RenderJsonRendering;
using Sitecore.LayoutService.Serialization;
using Sitecore.Mvc.Presentation;
using System.Linq;
@parrya
parrya / CorsHeaders.cs
Created October 22, 2021 03:20
CORS header code for redirects in layout service
HttpResponseBase response = filterContext.HttpContext.Response;
response.Headers["Access-Control-Allow-Origin"] = header;
if (string.IsNullOrWhiteSpace(response.Headers.Get("Access-Control-Allow-Headers")))
response.AddHeader("Access-Control-Allow-Headers", "*");
if (string.IsNullOrWhiteSpace(response.Headers.Get("Access-Control-Allow-Credentials")))
response.AddHeader("Access-Control-Allow-Credentials", "true");
if (string.IsNullOrWhiteSpace(response.Headers.Get("Access-Control-Allow-Credentials")))
response.AddHeader("Access-Control-Expose-Headers", "ETag");
if (!string.IsNullOrWhiteSpace(response.Headers.Get("Access-Control-Max-Age")))
return;
@parrya
parrya / RedirectPatch.config
Last active October 22, 2021 03:18
Patching redirects into the Layout Service pipeline
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<mvc.requestBegin>
<processor type="Mysite.Feature.Redirects.Pipelines.RedirectHandler, Mysite.Feature.Redirects" resolve="true"
patch:after="*[@type='Sitecore.LayoutService.Mvc.Pipelines.RequestBegin.ContextItemResolver, Sitecore.LayoutService.Mvc']"/>
</mvc.requestBegin>
</pipelines>
</sitecore>