7

We have custom facets for contacts and these facets are used for segmentation in lists (segmentation rules defined at '/sitecore/system/Settings/Rules/Definitions/Elements/Segment Builder').

Segmentation works fine if the condition type inherits TypedQueryableOperatorCondition<T, IndexedContact> and 'Text' of the condition item contains [operatorid,Operator,,compares to]

But now we need segmentation based on a boolean flag facet, so what I want to do is add this condition and see the text like where contact property is true without selecting 'compare operation' which is useless here.

The trouble is that Sitecore (v8 upd 4) does not even load the condition class if [operator] phrase is missing in the Text.

1

Disclaimer: I've never actually tried this myself. But looking at the code you're using, there are two base conditions for the conditions you're dealing with. And based on your own statement; it works when you use one of these - you just want to get rid of having to define an operator for your condition.

public abstract class TypedQueryableOperatorCondition<T, TItem> : OperatorCondition<T>, IQueryableRule<TItem> where T : QueryableRuleContext<TItem> where TItem : IObjectIndexers

and

public abstract class TypedQueryableStringOperatorCondition<T, TItem> : StringOperatorCondition<T>, IQueryableRule<TItem> where T : QueryableRuleContext<TItem> where TItem : IObjectIndexers

Both of these are operator based. Now - based on previous experience (and this is where I'm guessing, in fact) - Sitecore will only spawn your Conditions if the class satisfies a certain signature. And given both of the above are operator conditions, none of them will do what you want.

My proposal would be to create another base class:

public abstract class TypedQueryableWhenCondition<T, TItem> : WhenCondition<T>, IQueryableRule<TItem> where T : QueryableRuleContext<TItem> where TItem : IObjectIndexers

And then your concrete class like:

public class ContactPropertyCondition<T> : TypedQueryableWhenCondition<T, IndexedContact> where T : VisitorRuleContext<IndexedContact>

This should give you the required method signature and with any luck, Sitecore will instantiate it for you.

  • Thank you, Sitecore actually executes condition with such approach. Last minor thing to resolve is to find out why 'except when' condition mode returns all contacts including those which correctly filtered by 'when'. – Daniil Oct 18 '16 at 15:57
0

You can use Sitecore.Rules.Conditions.WhenCondition<T>:

using System;
using System.Linq.Expressions;
using Sitecore.Analytics.Rules.SegmentBuilder;
using Sitecore.ContentSearch.Analytics.Models;
using Sitecore.ContentSearch.Rules;
using Sitecore.Rules.Conditions;

public class BooleanCondition<T> : WhenCondition<T>, IQueryableRule<IndexedContact>
  where T : VisitorRuleContext<IndexedContact>
{
    public Expression<Func<IndexedContact, bool>> InitPredicate { protected get; set; }
    public Expression<Func<IndexedContact, bool>> ResultPredicate { get; protected set; }

    protected override bool Execute(T ruleContext)
    {
        // put your expression here:
        this.ApplyFilter(ruleContext, c => true);
        return true;
    }

    private void ApplyFilter(T ruleContext, Expression<Func<IndexedContact, bool>> expression)
    {
        this.ResultPredicate = (this.InitPredicate ?? ruleContext.Where).And(expression);
        if (this.InitPredicate != null)
        {
            return;
        }

        ruleContext.Where = this.ResultPredicate;
    }
}
  • I tried WhenCondition but the issue is that Sitecore does not execute it (even constructor) if I do not put [operator] text into Text field – Daniil Oct 17 '16 at 15:52

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.