To get Sitecore to resolve datasources using custom logic, you can add a pipeline processor to the resolveRenderingDatasource
pipeline in Sitecore.config
:
<resolveRenderingDatasource>
<processor type="Example.DataSourceService, Example"/>
</resolveRenderingDatasource>
The DataSourceService
processor can then inject any custom logic it needs for resolving the datasource string to an ID:
public class DataSourceService
{
public void Process(ResolveRenderingDatasourceArgs args)
{
if (!args.Datasource.StartsWith("site:"))
return;
if (Context.Database == null || Context.Site == null)
{
args.Datasource = "";
return;
}
var item = CustomQuery(args.Datasource);
args.Datasource = item.ID.ToString();
}
}
MVC:
If you're in an MVC environment you'll need to also add a processor to the mvc.customizeRendering
pipeline in Sitecore.MvcExperienceEditor.config
:
<mvc.customizeRendering>
<processor type="Example.ResolveRenderingDatasource, Example" patch:after="*"/>
</mvc.customizeRendering>
The ResolveRenderingDatasource
processor needs to call the resolveRenderingDatasource
pipeline.
Also make sure you add reference to Sitecore.Mvc.Analytics
.
using Sitecore.Mvc.Analytics.Pipelines.Response.CustomizeRendering;
using Sitecore.Pipelines;
using Sitecore.Pipelines.ResolveRenderingDatasource;
public class ResolveRenderingDatasource : CustomizeRenderingProcessor
{
public override void Process(CustomizeRenderingArgs args)
{
var rendering = args.Rendering;
var renderingDatasourceArgs = new ResolveRenderingDatasourceArgs(rendering.DataSource);
CorePipeline.Run("resolveRenderingDatasource", renderingDatasourceArgs);
rendering.DataSource = renderingDatasourceArgs.Datasource;
}
}
Sitecore.Pipelines.GetRenderingDatasource.GetDatasourceLocation
(check config forGetRenderingDatasource
pipeline). But it was ages ago when I modified things there so you need to dig more on your owngetRenderingDatasource
pipeline is only used when the datasource selection dialog is opened. I had attempted to modify theresolveRenderingDatasource
pipeline, but I couldn't figure out what changes might be necessary and I haven't seen much of anything online for handling this behavior.