Tuesday, March 23, 2010

Changing the default operator on the SharePoint Advanced Search

In one of my SharePoint projects, the customer asked the following question: “Can you add another operator in the property picker on Advanced Search aspx and can you change that operator into the default operator”?

Out of the Box advanced search

The out of the box Advanced Search page looks like this:

image

In the last field “Add property restrictions” you have 2 options in you default operator menu: the “Equals” and “does not equals” option. Before the Infra Structure update, you also had the options “contains” and “does not contains”. But Microsoft decided to remove this because these options can heavily decrease performance because expensive Transact-SQL queries are generated in this search scenario. For more information about this, read KB950437.

In my scenario, the customer wanted these options back and set the “contains” as the default operator.

Adding the ‘Contains’ operator

To do this simply go to the  Properties section of the Advanced Search Box configuration, click on the dialog box as seen below and then in the Properties xml on the top of the XML, change the default Value=”False”  into “True”:

     <Option Name=”AllowOpContains” Value=”True”/>

 image

After hitting the Apply button and saving this page, you get the two extra options:

image

Setting the ‘Contains’ operator as the default value

Now for the second requirement, I searched for a solution in Jquery which could be put in a hidden Content by Editor webpart. With the help of Marc D Anderson (follow him on Twitter @sympmarc) and his Jquery library for SharePoint Web Services website, I found the right code for this.

First of all, download the latest version of the jquery-1.4.2.min.js and put this in the document library that also contains the advanced.aspx. But be welcome to put in in any library of your choice. In the code underneath I make a reference to this jquery-1.4.2.min.js. BTW, always use the minified version in a production environment.

On the advanced.aspx page I have put a hidden Content editor webpart. In the source editor I put the following code, which results in setting the 'contains' operator as the default value:

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select[Title='Inclusion Operator'] option[Value='Contains']").attr("selected", "selected");});
</script>

This does the trick!

image

That’s a wrap

Without using heavy coding a power user can do many things on the SharePoint platform. You can extend your out of the box SharePoint with the help of using Jquery. On the internet you can find many resources with ‘ready to go’ Jquery scripts for your SharePoint platform and extend the SharePoint user experience. Be careful to test your code always in a test environment before putting it live on a production environment!

3 comments:

  1. This only seems to work on the initial page load, once I select a property e.g. "Author" it defaults back to "equals". Is there a way of fixing this?
    Thanks
    Michael

    ReplyDelete
  2. Same problem! On selecting Pick Property, Equals in replaced with 'Contains'. Let me know if someone resolved this issue.

    ReplyDelete
  3. I'm having the same problem as the other 2 people. Do you have any ideas on how this can be fixed?
    TIA
    Dean

    ReplyDelete