0

I am using Sitecore 9.3 JSS React Solution with Coveo. There is a custom computed field that contains the security assigned to the particular Sitecore page. The requirement is to match the field value with the user role value. If the match doesn't succeed then the page should be hidden from the search results.

I have been referring several javacripts to do the same referring to below links: JavascriptSearchFramework

I tried the below script:

document.addEventListener('afterInitialization', function () {
  if (typeof window !== "undefined")
  {               
      const root = document. Body;    
      var queryBuilder = new window["Coveo"].QueryBuilder();  
      root.addEventListener('buildingQuery', function(e) {            
      queryBuilder.advancedExpression.addFieldExpression('@securityinfo', '==', ['<roleName>'])        
      }); 

      window["Coveo"].init(root);
  }
});

Note: I already tried with the event "DOMContentLoaded" instead of "afterInitialization", but the function was not executing.

I am new in Coveo module, please guide me if I am doing anything wrong here. Thanks in advance.

1 Answer 1

0

I did this type of functionality by adding a filter in the search result. The requirement was to add a filter on the basis of a cookie. So I used the code to filter the result like this.

@using System.Web.Mvc.Html
@using Coveo.UI.Components
@using Coveo.UI.Components.Extensions
@Html.Partial(Partials.EDIT_TITLE, Html.Coveo().GetViewName())
 
@{
    HttpCookie regionCookieFilter = Request.Cookies["regionfilter"];
    var regionCookieFilterValue = string.Empty;
    if (regionCookieFilter != null && !string.IsNullOrEmpty(regionCookieFilter.Value))
    {
        regionCookieFilterValue = regionCookieFilter.Value;
    }
}
 
<div id="JSFilterExpression" class="CoveoForSitecoreFilterExpression"></div>
 
<script>
    document.addEventListener('DOMContentLoaded', function() {
    var currentPageExpressionBuilder  = new Coveo.ExpressionBuilder();
 
    var regionCookieFilterValue = '@regionCookieFilterValue';
    if (regionCookieFilterValue != "")
    {
        var fieldName = CoveoForSitecore.Context.fields.toCoveo('@@catalogitemregionavailable');
        currentPageExpressionBuilder.addFieldExpression(fieldName, '==', ['@regionCookieFilterValue']);
    }
 
    document.getElementById('JSFilterExpression').dataset.scAdvancedFilter = currentPageExpressionBuilder.build();
    });
</script>

It works perfectly for me. Try the above one and check.

For more details follow my article.

https://sbhatiablogs.wordpress.com/2020/10/01/coveo-for-sitecore-customization/

2
  • Thanks for your comment. Since I am using Sitecore jss solution with React application, I have created a component and added it to my search page. In the index.js file, my function with event listener 'DOMContentLoaded' is not at all hitting.
    – Tabbuu
    Jan 23 at 12:44
  • Did you try referring to this document? docs.coveo.com/en/3259/coveo-for-sitecore-v5/… Jan 23 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.