Query template replacer

From Sense/Net Wiki
Revision as of 09:41, 20 November 2015 by MiklosToth (Talk | contribs) (Custom query template replacer)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned


Sense/Net ECM has a powerful query language powered by Lucene. There are several features that we implemented to enhance the capabilities of Lucene, one of them is the query template mechanism. This lets you insert predefined parameters into a query that will be resolved at runtime, taking the current environment (the user, the context, the time) into account. This page is about how can you develop a custom template replacer class that adds custom query parameters to the list of built-in ones.


The query template replacer is built on the Template replacer technology we created to support replacing templates in any kind of scenario. In this article we describe only the behvior of the query template replacer.


When the portal starts, the Template replacer module discovers all the classes in the system that are derived from the TemplateReplacerBase base class. For details please check the Template replacer article.

When a query is executed, the portal checks the query for all the parameters above before parsing and executing the query. If any parameter is found in the text, the evaluator method of the appropriate template replacer class will be called and the result string (e.g. the current date or user id) will be inserted into the query text.

Template syntax

By default query templates are expected to be in the following format:

  • @@TemplateName@@
  • @@TemplateName.Field@@
  • @@TemplateName.Field1.Field2@@

For example:


There are helper methods in the replacer module for extending the behavior of these templates with expressions - e.g. adding or subtracting from a base value (+3days, -1week).


For more examples please check the Query template parameters article.

Custom query template replacer

The following example applies to Sense/Net ECM version 6.5.2 or higher. In versions before that, you have to inherit from the PortalLucQueryTemplateReplacer class.

To create your own query template replacer you only have to create a class that is derived from the PortalContentQueryTemplateReplacer class. There are two things that you should override:

  • TemplateNames property: a list of parameter names that you want to use in your queries (built-in examples are "currentsite", "currentuser")
  • EvaluateTemplate method: this is the method that does the evaluation of a parameter. You will receive an object name (one of the parameters above) and an optional property name(s).
  • You can also customize the way we parse and handle expressions, please check the main Template replacer article.

You have to evaluate the given parameter and return with a text that can be inserted to a query - e.g. a content path, ID, datetime value, or even with a full FieldName:SearchValue pair.

public class CustomQueryTemplateReplacer : PortalContentQueryTemplateReplacer
    public override IEnumerable<string> TemplateNames
        get { return new[] { "customfield", "myspecialvalue" }; }
    public override string EvaluateTemplate(string templateName, string templateExpression, object templatingContext)
        if (HttpContext.Current == null || PortalContext.Current == null)
            return base.EvaluateTemplate(templateName, templateExpression, templatingContext);
        switch (objectName)
            case "customfield":
                //return EvaluateExpression(myObject, templateExpression, templatingContext);
            case "myspecialvalue":
                //RETURN ANY QUERY TEXT HERE
                //return EvaluateExpression(myObject, templateExpression, templatingContext);
                return base.EvaluateTemplate(templateName, templateExpression, templatingContext);

Related links


There are no external references for this article.