Saturday, March 2, 2013

SharePoint 2013: Allowing Users to Sort Search Results

In previous versions of SharePoint, the out-of-the-box search results provided links to show results by relevance or modified date. Now, with SharePoint 2013, the results may be sorted by any managed property but there are a few preset values. However, by default, the sort option is not enabled. So to allow your users to sort search results, follow these simple steps:

1) Edit one of the results pages in your search center site and Edit the Search Results web part:
 
2) In the web part tool pane, scroll down and expand the Settings section. Click the Show sort dropdown checkbox:
 

The sort options are controlled by the JSON in the Available sort orders text box. You may modify this to add or remove sort options. Feel free to choose from any of the 600+ managed properties available out-of-the-box.

3) Click OK in the toolpane and check-in the changes of the page.

4) Perform a search. A drop-down appears above the results:


 

28 comments:

  1. THANK YOU!

    I don't understand why MS turned off so many useful features by default (Breadcrumb navigation being a notorious example), but your simple solution just saved me a lot of time.

    ReplyDelete
    Replies
    1. I believe this is off by default because there are so many other options in showing search results (such as result groups) that sorting may not make sense when using all of the other functionality. I'll keep posting as I work through our new search endeavors.

      Thanks for commenting!

      Delete
  2. I am doing a people search with a custom display template and am able to get the sort to work by first name with the following:

    [{"name":"First Name","sorts":[{"p":"Firstname","d":0}]}]

    If I try to add another option for last name which I have verified is a managed and crawled property and set to searchable, the web part errors out after selecting "Last Name" from the drop-down.

    This is what I am trying and I know the syntax is right as making them both say "FirstName" gives both options and neither error out.

    [{"name":"First Name","sorts":[{"p":"Firstname","d":0}]},{"name":"Last Name","sorts":[{"p":"LastName","d":0}]}]

    Any idea why this wouldn't work? I've tried various other managed properties with the same result.

    ReplyDelete
    Replies
    1. You have it set to "Sortable", correct? Is the property named LastName or Lastname? I'm pretty sure it is case sensistive.

      Delete
    2. Also, after you make a change on the managed property, you need to perform a full crawl to have it take effect.

      Delete
  3. It tuns out there is a "Sortable" checkbox that needs to be checked in the Search Schema Managed Properties for each item to make it available for sorting.

    ReplyDelete
    Replies
    1. Yes thank you! I was going to update this post with a note. You can't just stick any managed property into the JSON. It must be a managed property that has been marked as "sortable". You will receive an error when if you try to select a non-sortable property.

      -=Steve

      Delete
  4. Hey,
    I thought I'd ask, since I've been through this backwards and forwards:
    I am using the same sorting JSON as above, and the two First Name sorts work fine, but both Last Name sorts do not.
    I don't get an error, since the Last Name property is marked sortable, it just seems to be a random sort that is not based on alpha, social distance, dept, etc, but definitely not Last Name.
    Any ideas? Thanks!

    ReplyDelete
    Replies
    1. Send me the JSON and I'll look.

      Delete
    2. Did you run a full crawl after you changed the LastName property to Sortable?

      Delete
    3. I'm having the same issue. I mapped RefinableString00 to the same crawled properties as the LastName mapped property. Sort results by last name come out in random order

      Delete
    4. Try using Title for the sort. Contact content types rename Title to Last Name, but the internal name of the column is still Title, and the JSON probably needs to see Title in order to sort on it. (This applies to the similar question above from rogpodge as well.)

      Delete
    5. The sort entries should all match to the managed properties within the search schema. It doesn't matter what the internal name is behind the scenes.

      Delete
  5. We are setting up a people listing and adding sort by first and last name. We made last name sortable and did a full crawl. What we found is the sort is actually case sensitive, which does not work well for last names. So all uppercase letters come first, then lower case letters. So the last name de Hoya for example, actually comes after the last name Zylo. Is there anyway to make the sort non case-sensitive?

    Here's the JSON we are using:
    [{"name":"First name (A-Z)","sorts":[{"p":"FirstName","d":0}]},{"name":"First name (Z-A)","sorts":[{"p":"FirstName","d":1}]},{"name":"Last name (A-Z)","sorts":[{"p":"LastName","d":0}]},{"name":"Last name (Z-A)","sorts":[{"p":"LastName","d":1}]}]

    ReplyDelete
    Replies
    1. Create a SortLastName property and populate that with the LastName in all caps. Then use that as the sort property.

      Delete
  6. I'm assuming you are referring to creating a new search managed property and not a user profile property?

    What I can't figure out is how to transform the crawled property to all Caps. I do see a Token Normalization option in the managed property, but that looks like it's only relevant for search queries and it's already enabled in the LastName managed property.

    ReplyDelete
    Replies
    1. You would need both a custom user profile property and search property. There may be a better hack for this but I've done it this way with Bdc/Bcs sort issues.

      Delete
  7. If you want to use the sorting options in the "Change Query" dialog and not use the sort drop-down you can just set the JSON property to an empty array: []
    If you save that, then come back in and uncheck the box to show the sort drop-down, then you can change the sort order from the nice dialog screen! You have to have [] in there otherwise something gets screwed up and you can't sort from the dialog window.

    ReplyDelete
  8. I'm having a similiar issue as everyone else, I can sort on firstname and departments, however when I do a sort on last name I'm getting an error message:
    ttribute vector 'LastName' not available for sorting:

    and when I go into the Search Schema in CA, the LastName property is set to Sortable=yes:active

    ReplyDelete
    Replies
    1. Make sure in the JSON, it is capital L and capital N. Run a full crawl after changing the sort property.

      Delete
    2. I did all of that and no luck in sorting of the last name.

      Delete
    3. We're in the same position, did you find a resolution?

      Delete
  9. Hello Steve, not sure if my last post went through, so please forgive me for asking twice. How can we sort by file type? For example, I want all pages, or all PDF's, or all Word Documents? How easy would it be to set this up from the front-end of SharePoint? Thanks!

    ReplyDelete
    Replies
    1. You would need to add the file type property to the sort JSON. Also, you would need to make sure that property is set to Sortable. The property is used in the refinements so you can look at that web part configuration to find the proper property. I am on vacation this week so I can't look into that at the moment.

      I can personally assist you online if needed through my tutoring account: https://www.wyzant.com/Tutors/stevetheman

      Delete
  10. Hi Steve

    Just came across your article and thought you might be the right person for my query.

    I have a custom result source with no sorting and if show dropdown is checked, the dropdown appears but selecting any default options like modified date(Ascending) doesnt work.

    But if i copy the query from the result source directly on the webparts query builder dropdown selection sorts the resutls as expected.

    Any clues?

    thanks
    deepthi

    ReplyDelete
    Replies
    1. One may use {searchTerms} and one may use {searchBoxQuery}. Technically both should sort but sometimes there are unexplainable things in SharePoint. There could be a conflict with the query builder configuration within the Web part.

      Delete
  11. Hi Steve, How do you get it to sort by First Name then Last Name - at present it shows Bob Smith above Bob Andrews.

    Code is currently as follows: [{"name":"First name (A-Z)","sorts":[{"p":"FirstName","d":0}]},{"name":"First name (Z-A)","sorts":[{"p":"FirstName","d":1}]},{"name":"Last name (A-Z)","sorts":[{"p":"LastName","d":0}]},{"name":"Last name (Z-A)","sorts":[{"p":"LastName","d":1}]}]

    ReplyDelete
    Replies
    1. configure the sorting within the search results Web part query (Change Query button). but then changing the sort drop down may not work as expected if you select last name.

      Delete