Hide Sitecore Rendering Based On Querystring

                                           Hide Sitecore Rendering Based On Querystring


   GOAL: Need to Hide Sitecore Rendering based on matching Sitecore Field values to requested url querystring value

                          Sample test url will be https://local.dev.com/?hidebanner=bodybanner

        Step-1:

                Create Sitecore Item to configure querystring value and RenderingItemID Need to Hide. 


Step 2:

        Create Pipelines and code logic need to execute only CD environment not Sitecore CMS. so mention sitecore Role specific rule.

<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:azrenv="http://www.sitecore.net/xmlconfig/azrenv/" xmlns:azrrole="http://www.sitecore.net/xmlconfig/azrrole/">

  <sitecore role:require="Standalone or ContentDelivery" search:require="Azure">

   <pipelines>

      <mvc.getRenderer>

        <processor type="Test.Feature.HideComponent.HideRenderingBasedOnQueryString, Test.Feature.HideComponent" patch:before="*[position()=last()]">

        </processor>

      </mvc.getRenderer>

    </pipelines>

  </sitecore>

</configuration>

Step 3:

Custom logic need to implement for Hide Sitecore Rendering based on matching Sitecore Field values to requested url querystring value

sample test url will be https://local.dev.com/?hidebanner=bodybanner

public class HideRenderingBasedOnQueryString: GetRendererProcessor

{

  public override void Process(GetRendererArgs args)

  {  HideComponent();  }

  private void HideComponent() {

    Sitecore.Data.Database _currentDB = Sitecore.Context.Database;

    Item siteItem = _currentDB.GetItem("/sitecore/content/TestSite/Settings/HideRendering_Folder");

    if (siteItem != null) {

      Sitecore.Collections.ChildList settingItems = siteItem.GetChildren();

      if (settingItems != null & settingItems.Count > 0) {

        foreach(Item setting in settingItems) {

          if (ShouldComponentBeHidden(setting)) {

            Log.Info("Renderig Hide Sucessfully as per" + setting.DisplayName, string.Empty);

            HtmlCache cache = CacheManager.GetHtmlCache(Context.Site);

            if (cache != null) {

              cache.Clear();

            }

          }

        }

      }

    }

  }

  private bool ShouldComponentBeHidden(Item settingItem) {

    if (settingItem == null) {  return false; }

 var queryString = settingItem != null ? settingItem.Fields["Query String parameters"].Value : "";

    var rederingIds = settingItem != null ? settingItem["RenderingItemID"] : "";

    if (CompareQueryStrings(queryString) == true) {

      var pageContext = PageContext.Current;

      var pageDefinition = pageContext.PageDefinition;

   Rendering rendering = pageDefinition.Renderings.Where(x => x.RenderingItem.ID.ToString() == rederingIds.Trim()).FirstOrDefault();

 if (rendering != null) {

        rendering.Renderer = new EmptyRenderer();

        Log.Debug($"Hiding {rendering.Item.Name} (ID: {rendering.Item.ID}) rendering by {GetType().FullName} pipeline processor");

        return true;

      }

    }

    return false;

  }

  public bool CompareQueryStrings(string scQueryString) {

    NameValueCollection requestURLParam = HttpContext.Current.Request.QueryString;

    NameValueCollection sitecoreParam = WebUtil.ParseUrlParameters(scQueryString);

    var requestKeys = HttpContext.Current.Request.QueryString.AllKeys;

    var configKeys = sitecoreParam.AllKeys;

    foreach(string rkey in requestKeys) {

      if (configKeys.Any(x => x.Equals(rkey, StringComparison.CurrentCultureIgnoreCase))) {

        var requestValue = requestURLParam.GetValues(rkey).FirstOrDefault();

        var sitecoreValue = sitecoreParam.GetValues(rkey).FirstOrDefault();

        var splitsitecoreValue = sitecoreValue.Split(new [] { '|' },  StringSplitOptions.RemoveEmptyEntries).ToList();

        return splitsitecoreValue.Any(c => c.Equals(requestValue, StringComparison.CurrentCultureIgnoreCase));

      }

    }

    return false;

  }

}


Happy Coding!!!☺

Thanks for reading…..!!!Stay tuned for more….!!!

Comments

Popular posts from this blog

How to Create Sitecore Web Form