Rules Queries Pt II – Using Sitecore Rules Queries in Code

Ok, so – Cardinal Rules Queries has recently had a beta release, you can find it here. It allows you to leverage the power of the rules engine in Sitecore to build queries based on Lucene.Net, and if you choose – other search tools. Please see my initial post Sitecore Rules Queries Pt I for a bit more information.

Following on from this, I thought I would write a quick article on how you actually use it in code. Please note this is subject to change, but is correct at the time of posting.

At its core – its really simple, the engine leverages a QueryManager, this object is responsible for translating between Sitecore’s Rules structures and a useful result. I am in the process of allowing POCO returns, but I am using Sitecore Items in this example

            // realistically - DI this, but for clarity πŸ˜‰
            IQueryManager queryManager = new QueryManager(new LucinqSearchRepository(new ContentDatabaseHelper()), new ItemManager(new ItemRepository(), new SitecoreLog()), new SitecoreHelper());
            ValueRuleContext context = new ValueRuleContext(null, new Dictionary());


            RuleList rulesList = RuleFactory.GetRules(item, fieldName); // item is the query item you are using - it could easily be a datasource πŸ˜‰

            // not entirely sure this is needed - am still experimenting.
            rulesList.Run(context);

            IRulesSearchResult result = queryManager.GetRulesSearchResult(rulesList, new SearchOptions(0, 20));

            // do something with you results

So go ahead, create rules, drive code, set your criteria / actions using the rules engine πŸ˜€

Related Articles

Advertisements

One thought on “Rules Queries Pt II – Using Sitecore Rules Queries in Code

  1. Pingback: Rules Queries Pt III – What can we use rules queries for ? | cardinalcore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s