Skip to content

Change dropdown order for Search Action Links (sort by) web part.

10/29/2011

Not sure how this escaped me before, but I just noticed today that you cannot change the order of “sort by” properties in the Search Action Links web part.  This seems like it should be there so I reached out to my network to see if I was just missing something.  I ping’d search connector whiz Chris Even (http://sharepointsearch.com/) about this today and he said matter of factly “do it in javascript” – so I did.  And here is the result.

First off, if you don’t know what the Search Action Links web part is, this is the web part that, among other things, allows the user to change the sort order (or rank profile) of the results set.   You can add any managed property that has a Sortable=true property.  That’s really cool stuff but I was shocked to notice that you can’t choose what order those properties appear.  And they don’t even show up in alphabetical order (I’ll follow up on why not in another post).

Now I don’t claim to be a jQuery guy so don’t blast me on the code.  And this code can and should be expanded for a production implementation, but it works 😉

To use this, just paste it into a content editor web part on your search results page and define whether you want to sort alphabetically or define a custom order right at the top of the code.

<script type=”text/javascript”>
//SET ‘mySort’ TO “Alpha” FOR ALPHABETICAL.  SET TO “Custom” TO USE AN ARRAY OF PROPERTIES.

//var mySort = “Alpha”;
var mySort = “Custom”;

$(document).ready(function(){
if(mySort == “Custom”){
//ADD ALL PROPERTY NAMES IN THE ORDER YOU WANT TO SEE THEM
var arrMyOrderedVals = [“Date (Oldest)”, “Relevance”, “Date (Newest)”];
jQuery.each(arrMyOrderedVals , function() {
$myOption = $(‘#dropdown option:contains(‘+this+’)’);
$(‘#dropdown option:contains(‘+this+’)’).remove();
$(‘#dropdown’).append($(“<option></option>”).attr(“value”,$myOption.val()).text($myOption.text()));
});
}
else if(mySort == “Alpha”){
$(“#dropdown”).each(function() {
// REMEMBER SELECTED VALUE
var selectedValue = $(this).val();
// SORT ALL OPTIONS BY TEXT ATTRIBUTE
$(this).html($(“option”, $(this)).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}));
// RE-SELECT VALUE
$(this).val(selectedValue);
});
}
});
</script>

Hope this gives you an idea of how to accomplish this task that will come up for a lot of you at some point or another!

Advertisements
Leave a Comment

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

%d bloggers like this: