I hope you would have seen the article already regarding the wildcard using item resolver before reading this.
So, to resolve the wildcard using the data source resolver the idea is that this time we will apply the rendering, or presentation detail on the * item instead of the data source item unlike shown in the previous blog.
So our structure will be now as follows:
- Home
- Countries
* (with presentation detail with your controller rendering available)
we will keep the data items in the global folder as
- Data
- Countries
India - (with no presentation details)
USA - (with no presentation details)
Russia - (with no presentation details)
Now, when URL URL https://[domain]/countries/india maps to the * item then we will run a processor to resolve the datasource dynamically on the basis of country passed in the URL.
So, in processor we will say that in the rendering dynamically pass the datasource as /sitecore/content/POC Site/New site/Data/Countries/{last segment of URL}. Here in the example last segment of the URL will be India so the complete datasource on runtime will be /sitecore/content/POC Site/New site/Data/Countries/india.
Here is the code below:
using Sitecore.Mvc.Pipelines.Response.GetRenderer;
using System;
using System.Linq;
namespace Feature.WeatherForecast.Pipelines
{
public class CountryDatasource : GetRendererProcessor
{
private string RenderingName { get; set; }
public override void Process(GetRendererArgs args)
{
var productsDetailRendering = RenderingName;
if (args.PageContext.Item.Name != "*"
|| !args.Rendering.RenderingItem.Name.Equals(productsDetailRendering, StringComparison.InvariantCultureIgnoreCase))
return;
if (args.PageContext.RequestContext.HttpContext.Request.Url == null) return;
const string datasourceFolder = "/sitecore/content/POC Site/New site/Data/Countries";
var dataSourcePath =
$"{datasourceFolder}/{args.PageContext.RequestContext.HttpContext.Request.Url.Segments.Last()}";
var dataSourceItem = Sitecore.Context.Database.GetItem(dataSourcePath);
if (dataSourceItem != null)
{
args.Rendering.DataSource = dataSourcePath;
}
else
{
var notFoundItem = Sitecore.Context.Database.GetItem("/sitecore/content/YourSite/NotFoundPage");
if (notFoundItem != null)
{
// Set the 404 page item as the current item
Context.Item = notFoundItem;
}
else
{
// Log a warning if the 404 page item is not found
Log.Warn("404 Page Not Found item not found in Sitecore content tree.", this);
}
}
}
}
}
Finally patch your changes:
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.getRenderer>
<processor type="Feature.WeatherForecast.Pipeliness.CountryDatasource, Feature.WeatherForecast" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']">
<renderingname>WeatherForecast</renderingname>
</processor>
</mvc.getRenderer>
</pipelines>
</sitecore>
</configuration>
Hope it helps!
http://sitecorepeanuts.blogspot.com/2024/03/wildcard-page-in-sitecore-using.html
ReplyDelete