• Not Answered

How to invoke a Condition Rendering Rule from Code

Hi to  everybody dear collegues,

I have a Conditional Rendering Rule that return true or false, I'd like to call it from code but I don't know if there is a way for doing that.

There is some one that can help me?

Thanks a lot.


1 Reply

  • Hi guys I fund the answer. Here are the code that we can use to execute a Conditional rendering rule from code:

    Item item = Sitecore.Context.Item;
    Sitecore.Data.Database contextDB = Sitecore.Configuration.Factory.GetDatabase(item.Database.Name);
    Sitecore.Data.Items.Item ruleItem = contextDB.GetItem(new Sitecore.Data.ID(SitecoreConstants.ItemGUID), item.Language);
    String rule = ruleItem.Fields["Rule"].Value;
    var rules = RuleFactory.ParseRules<RuleContext>(item.Database,XElement.Parse(rule));

    var ruleContext = new RuleContext()
    Item = item

    if (rules.Rules.Any())
    return rules.Rules.First().Evaluate(ruleContext);