Optimizing Layout Service: Exposing Sitecore Search Configurations for Each Page in XM Cloud
Share those precious objects! Go beyond the page-level
fields and expose what the Layout service really wants. Be a hero, not a hoarder 😉.
In our project, we use XM
Cloud and Sitecore Search, with all configurations stored in the site
configuration item under site settings. Let's expose these search
configurations, and potentially other fields, to the Layout service for each
individual page. Here's how we can do it:
1. Locate the Configurations: Identify where all the Sitecore
Search configurations (or other fields) are stored within your site settings.
These are your golden keys.
2. Expose the Configurations: Ensure that these configurations are
exposed to the Layout service. This is like opening a treasure chest and
sharing the wealth with each individual page.
3. Update the Layout Service: Modify the Layout service to include
these configurations for each page. This way, every page gets exactly what it
needs to function optimally.
By following these steps,
you'll ensure that every page in your XM Cloud project has access to the
necessary Sitecore Search configurations, or any other fields you wish to
expose, through the Layout service. This approach isn't limited to search
configurations; it can be applied to expose any additional fields not part of
the context item layout service.
Without wasting time let’s start with overriding the process
Step-1:
Create a processor file named SiteCofiguration.cs and we need to override the
process of IGetLayoutServiceContextProcessor
using Newtonsoft.Json.Linq;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using
Sitecore.LayoutService.ItemRendering.Pipelines.GetLayoutServiceContext;
using Sitecore.Links.UrlBuilders;
using Sitecore.Links;
using System.Collections.Generic;
using Sitecore.Mvc.Extensions;
namespace
Company.Foundation.SitecoreExtensions.PipelineProcessors
{
public class SiteConfiguration : IGetLayoutServiceContextProcessor
{
public void
Process(GetLayoutServiceContextArgs args)
{
if (Sitecore.Context.Site
!= null)
{
//As we have all the configurations under settings item,
here we are getting the settings item
Item SettingItem =
Sitecore.Context.Database.GetItem(new ID("{EE3244BB-660C-4A6B-XXXX-XXXX1D95A33A}"));
//Getting item where we store all search related
configuraton
var siteConfigurationItem =
SettingItem.Axes.GetDescendants().Where(x => x.TemplateID == new
ID("{BFD2102F-XXXX-XXXX-ABEE-C41978D1CB25}")).First();
if (siteConfigurationItem
!= null)
{
//args.ContextData.Add("Name
that should display in layout service", value)
args.ContextData.Add("GTMContainerID",
siteConfigurationItem.Fields[GTMContainerID]?.Value);
args.ContextData.Add("SearchAPIURL",
siteConfigurationItem.Fields[SearchApiUrl]?.Value);
args.ContextData.Add("SearchSource", siteConfigurationItem.Fields[SearchSource]?.Value);
}
}
}
}
}
Step-2: Let’s add the processor in config file
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<group groupName="layoutService">
<pipelines>
<getLayoutServiceContext>
<processor type="Company.Foundation.SitecoreExtensions.PipelineProcessors.SiteConfiguration,
Company.Foundation.SitecoreExtensions" />
</getLayoutServiceContext>
</pipelines>
</group>
</pipelines>
</sitecore>
</configurations>
Let’s build the solution and start deployment in XM Cloud
Deploy app.
Sample Output:
Once you’ve exposed the objects, test it out. Make sure
the Layout service is dancing with joy (or at least not throwing error messages
at you). Celebrate your success with a victory dance of your own.

Comments
Post a Comment