Alfresco Share – Customize advanced search for aspect properties

The advanced search form in Alfresco Share allows to use specific property values to search depending on the content type selected. Alfresco wiki includes a detailed explanation on configuring these feature and Jeff Potts develops a sample on his Working With Custom Content Types tutorial.

The problem

As Axel Faust describes at Alfresco Forum: “currently it’s not possible to setup a search form based just on an aspect. The reason for this is that there is no FormProcessor on the Repository tier that can handle aspect-based forms”.

The hack

AdvancedSearch Javascript component can be overridden in order to include aspect properties as search fields just prefixing the property with “prop_”.

(function() {
	
	var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, Selector = YAHOO.util.Selector, Bubbling = YAHOO.Bubbling;
	var $html = Alfresco.util.encodeHTML, $links = Alfresco.util.activateLinks, $combine = Alfresco.util.combinePaths, $userProfile = Alfresco.util.userProfileLink;

	Alfresco.ExtendedAdvancedSearch = function(htmlId) {
		return Alfresco.ExtendedAdvancedSearch.superclass.constructor.call(this, htmlId);
	};

    YAHOO.extend(Alfresco.ExtendedAdvancedSearch, Alfresco.AdvancedSearch);

	YAHOO.lang.augmentObject(Alfresco.ExtendedAdvancedSearch.prototype, {
		
	    onSearchClick: function ADVSearch_onSearchClick(e, obj)
	    {
	    	
	       // retrieve form data structure directly from the runtime
	       var formData = this.currentForm.runtime.getFormData();
	       
	       // add "prop_" prefix to aspect properties
	       var modifiedFormData = {},
               length = formData.length;
				       
		   var originalValue;
		   for (var prop in formData) {
			    if (formData.hasOwnProperty(prop)) {
		        	originalValue = formData[prop];
			        if (!(prop.substring(0, 5) == "prop_")) {
			        	prop = "prop_" + prop;
			        }
			        modifiedFormData[prop] = originalValue;
			    }
		   }
		   formData = modifiedFormData;
	       
	       // add DD type to form data structure
	       formData.datatype = this.currentForm.type;
	       
	       // build and execute url for search page
	       var url = YAHOO.lang.substitute(Alfresco.constants.URL_PAGECONTEXT + "{site}search?t={terms}&q={query}&r={repo}",
	       {
	          site: (this.options.siteId.length !== 0 ? ("site/" + this.options.siteId + "/") : ""),
	          terms: encodeURIComponent(Dom.get(this.id + "-search-text").value),
	          query: encodeURIComponent(YAHOO.lang.JSON.stringify(formData)),
	          repo: this.options.searchRepo.toString()
	       });
	       
	       window.location.href = url;
	    }
		  
	}, true);
})();

Alternative workaround

You can define and advanced search form based on an aspect without modifying the product and without adding extra fields. Just defining a new advanced search form based on cm:content but including only aspects fields with the modifier force.

1. Create a new entry in advanced search forms list

	<config replace="true" evaluator="string-compare" condition="AdvancedSearch">
	    <advanced-search>
	        <forms>
                    <!-- Named search form to avoid default "search" form from cm:content -->
	            <form id="custom-search" labelId="advsearch.label.custom" descriptionId="advsearch.description.custom">cm:content</form>
	            <form labelId="search.form.label.cm_content" descriptionId="search.form.desc.cm_content">cm:content</form>
	            <form labelId="search.form.label.cm_folder" descriptionId="search.form.desc.cm_folder">cm:folder</form>            
	        </forms>
	    </advanced-search>
	</config>

2. Define your form based on cm:content but including only aspects fields and using force. Below a simple my:field from fake aspect is configured.

<config evaluator="model-type" condition="cm:content">
    <forms>
        <form id="custom-search">
            <field-visibility>
                <show id="my:field" force="true" />
            </field-visibility>
            <appearance>
                <field id="my:field">
                    <control template="/org/alfresco/components/form/controls/textfield.ftl" />
                </field>
            </appearance>
        </form>
    </forms>
</config>

Published by angelborroy

Understanding software.

5 thoughts on “Alfresco Share – Customize advanced search for aspect properties

  1. And how to use it?
    Can you provide a sample share form config to use with your hack?
    How do you deploy this modified javascript hack?
    Thank you for sharing your solution.

    1. You can use it as a part of a Share AMP, including this JS in components/search/extended-advsearch.js and redefining references at web-extension/site-webscripts/org/alfresco/components/search/advsearch.get.js and web-extension/site-webscripts/org/alfresco/components/search/advsearch.get.html.ftl.

      By using “Working With Custom Content Types” source code from Jeff Potts it can be enough. It works on every custom aspect, so no specific model must be defined.

      Thanks for your feedback!

  2. Soy nuevo en al-fresco y quiero tener un modo de buscar archivos de forma eficiente ya que la empresa donde vamos implementar un flujo de archivos e imagenes importantes y queremos buscar archivos de forma mas eficientes

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s