Create Custom Pipeline Processor for Queryable Source of Sitecore Rendering Parameter Template Field

When defining the lookup source of Droplink or Treelist in a Sitecore template, It is very straightforward to use Sitecore query to specify the root path and template Id/name in a regular standard template. For example:

query:./ancestor-or-self::*[@@templatename='MyTemplate']

But it doesn’t work in Rendering Parameter Template as not querying in the context content. It fails and just list entire content tree.

Here is a scenario in Sitecore multisites:

  • Global level content is created and cloned to several sites with same structure, same item names and same templates
  • Each site has function using shared common rendering parameter template.
  • There are Droplink/Treelist fields in the rendering parameter template.
  • The source of Droplink/Treelist fields are defined in lookup of cloned content in the scope of current site.

The key to solve the issue is, make sure the queryable lookup is in the current site context content. Let’s create a custom processor to add to Pipelines to tell Sitecore to get context item when lookup in rendering parameter template.

using System.Linq;
using Sitecore.Data;
using Sitecore.Pipelines.GetLookupSourceItems;
using Sitecore.Shell.Applications.ContentEditor;
using Sitecore.Text;
using Sitecore.Web;

namespace MySitecoreProject.App_Help.LookupSource
{
   public class ParameterTemplateSourceQueries
   {
      // Standard Rendering Parameters template ID
      private readonly ID _baseParameterTemplate = new ID("{8CA06D6A-B353-44E8-BC31-B528C7306971}");

      public void Process(GetLookupSourceItemsArgs args)
      {
         // Only patch to Sitecore query
         if (!args.Source.StartsWith("query:"))
         {return; }

         if (args.Item.Template.BaseTemplates.All(bt => bt.ID != _baseParameterTemplate))
         {return;}

         var url = WebUtil.GetQueryString();
         if (string.IsNullOrWhiteSpace(url) || !url.Contains("hdl"))
         {return;}

         var parameters = FieldEditorOptions.Parse(new UrlString(url)).Parameters;

         var currentItemId = parameters["contentitem"];
         if (string.IsNullOrEmpty(currentItemId))
         {return;}

         var contentItemUri = new ItemUri(currentItemId);
         var contextItem = Database.GetItem(contentItemUri);

         args.Item = contextItem;
       }
    }
}

Create a configuration file under “App_Config\Include” and add processor patch to “getLookupSourceItems” before “Sitecore.Pipelines.GetLookupSourceItems.ProcessQuerySource”.

<pipelines>
    <!-- Lookup Source -->
   <getLookupSourceItems>
       <processor patch:before="*[@type='Sitecore.Pipelines.GetLookupSourceItems.ProcessQuerySource, Sitecore.Kernel']" type="MySitecoreProject.App_Help.LookupSource.ParameterTemplateSourceQueries, MySitecoreProject" />
   </getLookupSourceItems>
</pipelines>
This entry was posted in Information Technology, Sitecore and tagged , , , , , . Bookmark the permalink.

3 Responses to Create Custom Pipeline Processor for Queryable Source of Sitecore Rendering Parameter Template Field

  1. Shashi says:

    Thank you solution works fine but broken links error comes when i saved. Any suggestions?

  2. shasanji says:

    thank you very much getting back to me once i change the value from page(not template) let’s home page or some other page usual pop box come up with the Broken links error message and show the field name. But once i change the control Multi-list (used to be drop-down or Drop-link ) error gone.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s