Multi-valued search in Alfresco

Introduction

The purpose of this blog is to show how to enable search for multi-valued properties in advanced search form in Alfresco. Let’s assume that we are looking for authors and we want to find all the documents created by author John or Mary. This feature allows to do this in single query instead of two queries.

Solution

Multi-valued search is already supported in Alfresco, but requires additional configuration. This can be done by adding additional hidden field to advanced search form. The field should follow the name format prop:{name of the filed with values separated by commas}-mode and defines logical operator that should be used to replace commas.

 
   <config evaluator="model-type" condition="cm:content">
        <forms>
            <!-- Search form -->
            <form id="search">
                <field-visibility>
 
                    <!-- Author field-->
                    <show id="cm:author" force="true" />
                    <!-- Field to enable multi-value author support-->
                    <show id="prop:cm:author-mode" force="true"/>
 
                </field-visibility>
                <appearance>
 
                    <field id="cm:author" set="document_additional"/>
                    <!-- Use OR logical operator for authors -->
                    <field id="prop:cm:author-mode" set="document_additional">
                        <control template="/org/alfresco/components/form/controls/hidden.ftl">
                            <control-param name="contextProperty">OR</control-param>
                        </control>
                    </field>
 
                </appearance>
            </form>
        </forms>
    </config>

In addition, hidden.ftl form control has to be modified to support providing field value in contextProperty parameter. The changes are presented below:

<#-- Renders a hidden form field for edit and create modes only -->
<#assign fieldValue = "">
<#if field.control.params.contextProperty??>
   <#if context.properties[field.control.params.contextProperty]??>
      <#assign fieldValue = context.properties[field.control.params.contextProperty]>
   <#elseif args[field.control.params.contextProperty]??>
      <#assign fieldValue = args[field.control.params.contextProperty]>
   <#else>
       <#assign fieldValue = field.control.params.contextProperty>
   </#if>
<#elseif context.properties[field.name]??>
   <#assign fieldValue = context.properties[field.name]>
<#else>
   <#assign fieldValue = field.value>
</#if>
 
<#if form.mode == "edit" || form.mode == "create">
   <input type="hidden" name="${field.name}" 
          <#if field.value?is_number>value="${fieldValue?c}"<#else>value="${fieldValue?html}"</#if> />
</#if>
  1. Romain Delavault

    Hi,

    thanks for this post. This is -I think- what I am looking for, but I am still lost. Those pieces of code, seem to not be enough in my case for the multi-valued search to work, and I can’t find anything relevant on Alfresco forums, youger than january 2012 (all I could find being still unanswered).

    1) I understood, browsing Alfresco forums, that such text fields (allowing multiple values separated by commas) would need, in the model, to be a property owning a true parameter. In this example, the property cm:author in the model would look like this :

    Author
    d:boolean
    true


    Did you have to do that for your example (or was it already done) ?

    2) If this is the case, it is a problem because my model is already defined, and must not change (especially some fields, I think it would mess up the entire project).
    Hence my next question : do you know any way to do it without having to change the model ?

    3) If this isn’t the case, maybe did I miss something during the process ; do you have any idea what could that be ?
    - I added a hidden field to my advanced search form (had to remove the parameter force=”true”, not sure what this does yet, but it made the field I want to search on disappear) ;
    - I added a control-parameter with my operator (same as yours, OR) ;
    - my hidden.ftl is basically the same as yours already.

    4) What happens if the string I am looking for contains a comma in it (say, a title, for example) ? Since the separator is the comma, wouldn’t it be a problem ? Or would the user doing the request have to use quotes in order to search for exact values ?

    5) What does your request look like, what string do you put in the field in order to get what you expect ?

    Thanks in advance for your answers.
    Cheers,

    Romain

Leave a Comment


*


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">