Gravatar for diego.moretto@xcentium.com

Question by Diego, Jan 14, 2015 11:12 AM

Help building custom rules

Hello

I have a sitecore project I'm building in which I need the search to be filtered by the site where the user is at. The site structure is something like this:

Site A /sitecore/content/siteA/SearchPage

Site B /sitecore/content/SiteB/SearchPage … Site Z /sitecore/content/SiteZ/SearchPage

So we are building microsites and we are looking at having a lot of those microsites but we need to filter the search results for those microsites only. so if the user is at siteA, only the content within that site should be showing up on the search results and so on.

I considered creating a custom rule for sitecore and apply on the searchpage template and every single of those items will have that rule. The rule I'm trying to build essentially needs to get the current item which will be the searchpage and look for the parent of it(SiteA, SiteB, etc) and get it´s descendants. I built and extension method that gets the descendants:

public static List GetDescendants(this Item item) {

        return new List<Item>(Context.Database.SelectItems(item.Paths.LongID + "//*"));
    }

So if I do something like this: var d = ruleContext.Item.GetDescendants(); I get all the descendants for that item.This works just fine. I also tried to follow this tutorial but I'm not sure how the GetQueryExpression and the Execute method should look like in order to achieve that. https://developers.coveo.com/display/public/SC201410/Creating+Custom+Filtering+Rule+Conditions+in+Sitecore

I thought in maybe comparing by parent path but again I should be checking for its descendants so I'm open for suggestions I am familiar with Linq but not much with linq expressions. Anyone ever done anything like this?

thanks

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Jan 14, 2015 2:31 PM

The rule you want to create is very similar to the "where item is the specific item or one of its descendants" rule but it does not have a configurable item and the item must be dynamically found using the search page parents.

You can create a "where the item is the site root item or one of its descendants" rule by inheriting from the rule above like this:

public class WhenIsSiteRootOrDescendant<T> : WhenIsDescendantOrSelf<T> where T : RuleContext {
    public WhenIsSiteRootOrDescendant() {
        // Force the WhenIsDescendantOrSelf<T>.ItemId to the site root item ID.
        ItemId = GetSiteRootItemID();
    }

    private ID GetSiteRootItemID() {
        // TODO: Implement this method with your custom logic to find the current item root item.
    }
}

Coveo for Sitecore already supports the WhenIsDescendantOrSelf<T> rule and generates the appropriate Expression for it. Coveo for Sitecore will detect your new rule as a WhenIsDescendantOrSelf<T> rule because it inherits from it and will also generates the appropriate Expression for it

In Sitecore, your new "Site Root or Descendant" rule will have the following field values:

  • Text: where the item is the site root item or one of its descendants
  • Type: Your.Namespace.WhenIsSiteRootOrDescendant,Your.Assembly
Gravatar for diego.moretto@xcentium.com

Comment by Diego, Jan 14, 2015 3:02 PM

thank you jflheureux

A couple of questions though. should I need an Execute method for this? In the constructor or in that GetSiteRootItemID method I don´t have the context to get the item where I set the rules, in this case the search page. The only place I can set a context from what I understand is through the Execute method with the context parameter. thanks

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jan 14, 2015 4:08 PM

You can simply use Sitecore.Context.Item.

private ID GetSiteRootItemID() {
    // TODO: Implement this method with your custom logic.
    return FindSiteRootItemFromChildItem(Sitecore.Context.Item).ID;
}
Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jan 14, 2015 4:09 PM

The Execute method is only used by Sitecore. Coveo for Sitecore never calls the Execute method on the rules.

Ask a question