Friday, February 5, 2016

Making SharePoint Search Results even better for your users

If you've only ever had even just a cursory glance at any SharePoint 2013 or SharePoint On line demo, you'll have noticed the nice-looking Search Results that come Out-of-Box nowadays with their sleeker look and hover previews of the documents themselves.  With the latest features we have access to numerous new components that make our results look better for end users, much easier for Power Users to change, and especially easier for us developer's to enhance than the black magic we had to use on previous versions (XML, XSLT was not your friend).
 
These new capabilities have allowed for Search-driven pages to be built out where all the links and information can be pulled directly from the Search Index instead of just List Views.
 
 
Just to review, here's a quick rundown of the features we've been able to leverage right away since the launch.
  • Delegated Search enhancements by Site Collection / Site (don't need to be a Farm Admin any more!)
  • FAST Search is integrated into enterprise search
  • New Search Web Parts: Search Results Web Part, Refinement Web Part or Panel, and Content Search Web Part
  • Result Types & Display Templates, Result Sources , and Query Rules
  • List or Library on-demand full crawl
  • Continuous Crawl, if you have the hardware and need for the freshest results
Search display templates have been decoupled in this version so that we have Control Display Templates to structure the overall layout for how we want to present the search results, while Item Display Templates determine how each item in the result set will be displayed.  There's also Item Hover Display Templates that show Page or document previews, but that's a different topic for another day.  For this article we'll just be targeting the Item templates as the standard structure is just fine.
 
But as with anything related to SharePoint, there's always more that can be enhanced to make our end users happier, and Search Results are no exception.  In this article, I'm going to show how easy it is to grab document / List metadata that's already being indexed and then update our Search Page results with that data.  My example is using a Search Results Web Part on a Page that has it's settings modified to just pull the latest document updates in the Site (no Keyword Search Box), but these template updates automatically apply to those setups that have a Search Box Web Part too.
 
I'm going to have the Last Modified date show on all results, as it should be part of every default template, in my humble opinion.  We'll also add in a Managed Metadata column that's attached to a specific Document Library, but only build a table of the data if the result comes from that Library.
 
Note:  To accomplish these updates you'll need to be a Site Collection Admin if you want to apply this for all your Sites/SubSites, or at least a Site Owner to make changes for just your Site.
 
Find your data
This will start out being the hardest part of the exercise as it's up to us (or from user requests) to determine what extra data we want to show, mapping it up in the Search Schema, and then figuring out how to display it.
 
Now that I've made you nervous, let's just jump in and take our first step to determine what extra data we want to show in our Search Results.  Usually it's a metadata column in our Document Library's that describes where, when, or how to use this document in some situation or project.  In my case I was asked to show the Template Type and Service Type data in the result item when any of the documents from this Library showed up.  While these are Managed Metadata columns, any type of data that is attached to an item can be used.
 
 
So now that we know what data is needed, it's time to put on our detective hat and go look for it in SharePoint's Search Schema, which is a collection of Crawled Properties of everything stored in our Sites that are then sometimes automatically mapped to Managed Properties for use (Site Columns assigned to Document Library's and Managed Metadata columns). To utilize indexed values in Search Results, we need to find or create the Managed Property that is mapped to a Crawled item.  When you go into Site Settings, under the Search header click on the Schema link.
 
 
Once in the properties interface, it will default to showing all the Managed Properties SharePoint creates OoB (Out-of-Box).  In the Search box, we'll type the beginning of one of the columns from the Document Library above, remembering that less text is more results and that SharePoint abhors spaces in column names (it replaces them with "_x0020_") and will fix them for you.
 
In the image below, you can see that our query returned the Managed Property we need called 'owstaxIdTemplateType', that is set for a Mapped Crawled Property named 'ows_taxId_TemplateType'.  This is where the detective work comes in if the target column wasn't given a name that is specific enough.  In this case I know the column/crawled property is a Managed Metadata column (e.g. "..._taxId_..."), and that the column was created with no spaces so that must be the one I want.
 
Now that we have our Managed Property, we need to look at the different attributes applied to them, such as Type, Query, Search, and Refine as well as others.  What we're looking for is to ensure that our property has the word "Retrieve" in the Retrieve column, otherwise we won't be able to show it's values in our updated results.  If it doesn't, we'll have to create our own Managed Property, but that's another article in the future.
 
 
Setup a Result Type
Whew, that was a lot of work finding our column in the index, but now that we have it things start getting easier.  Our next step is to copy an existing Item Template and change it from there (no sense in re-inventing the wheel).  I use SharePoint Designer 2013 and the Site Settings menu's for this, but you can use standard file explorer as noted in Design Manager capabilities (yet another article for the future).
 
To start our template copy, we first need to see what template SharePoint is using currently.  Back in Site Settings under the Search heading, click on the Result Types link. 
 
 
This will load the menu showing all the Display Templates by type, assigned to results that SharePoint shows for us.  We'll be creating versions for all the document types that are in our Library, but I'll just start with Excel for now.  Also, on the far right under Result Actions, notice that it says it uses 'Excel Item' which is what we'll need later to make a template copy.  Hover over the item, then in the hover dropdown, select the Copy function.
 
Note:  It's always best practice to make a copy of SharePoint objects and not update the original directly.  You never know if an update from Microsoft will help out and overwrite your changes.
 
 
Once the Edit Result Type page loads, in the name box append some text to explain how it's different (e.g. Site it's attached to, group, application, etc.) and note the sources it's hooked to and what type of content it matches are automatically selected.  Under the Actions is a "What should these results look like?" dropdown that is hooked to Excel Item already.  We'll update this with our new template later, but for now we have our entry for displaying results differently.  Click the Save button to create the entry which will post back to the Manage Result Types page showing our new entry.
 
 
 
Create & Enhance your (Item) Display Template
With our new Result Type created, we need to now create a copy of the core 'Excel Item' template to update and then attach.  We'll open SharePoint Designer to our Site URL then click on the All Files object in the left pane to show everything.  Since the templates are kind of hidden by SharePoint click on the _catalogs folder, then the masterpage folder, the Display Templates folder, and finally the Search folder.  This is the physical-ish location of all the Control and Item Display Templates.
 
 
Since SharePoint likes to keep things interesting, the Title of our target is 'Excel Item' but the file name pattern is reversed, as 'Item_Excel.html'.  The other interesting piece is you'll notice a .html AND a .js version of these files.  This new decoupled model takes our HTML template and compiles it down to JavaScript for us as we save and uses that JavaScript file when rendering the results.  So to make our version to work with, right-click on only the .html version of these files and select the Copy function, then right-click anywhere in the Library and choose the Paste function.  Once Designer finishes creating the copy in the Library at the bottom, rename it's file name by removing "copy(1)" and appending a distinctive name, as well as updating the Title column to the right (otherwise you'll see the "Excel Item" template twice when we go back into Site Settings and won't know which to pick).
 
 
Now we need to right-click on only our new .html file and select 'Check-Out', and then 'Edit File in Advanced Mode' to open it for editing.
 
 
With the file open in Edit mode, locate the XML node labeled "mso:ManagedPropertyMapping" near the top.  This is where we add in our Managed Property that we want the template to show.  You'll see that the syntax is pretty easy in that you put property name, colon, and a descriptive name for it (,'xxx':'xxx') at the end of the node with both pieces surrounded by single quotes.  I always just use the property name in the descriptor to make life easier.  In our case here, I'm adding in the value ",'owstaxIdServiceType':'owstaxIdServiceType'" just before the closing tag of the XML node.
 
After you update the Managed Property node, scroll down to just under the HTML that says <div id="Item_Excel">.  Here you'll see what looks like JavaScript comment tagging, but with extra pieces; _# and #_.  This is part of SharePoint's new compiler marking that tells it what to rebuild down into the .js file.  In the upper highlighted section I add in my Javascript where I set a few variables to use those new Managed Property's we added above, then evaluating if they're empty to determine whether to create my display table (checking if this item is in my Library where that metadata is set).  Finally, near the bottom of the file under SharePoint's default HTML I add in 2 new HTML elements for the dynamic table, and the Last Modified date that I want to show all the time.  Make sure you save and close the file when done.
 
Note:  To show the JavaScript in the HTML we have to surround our variables with the compiler notation mentioned above. " _#variable-name-here#_"
 
 
Update the Search Settings
Our next step is to go back to the browser in Site Settings and under the Web Designer Galleries heading, click on the Master Pages and Page Layouts link.  Here we'll Check-In and Publish a major version of the new template.
 
 
This will drop us into the Library where all the templates and layouts are stored.  Double-click on the Display Templates folder, then the Search folder.  This is where all the Control and Item Display Templates live within the Site Collection.  After paging to the 2nd set of results we'll see our new .html Item Template showing right around the existing one we copied from.  Hover over just the .html file and in the hover dropdown, select the Check In function.
 
 
Once the Check-In modal loads, select the 1.0 Major version (publish) choice that will make this version visible to everyone else once we've updated our Result Type.  In the template library we won't see the file as Checked-Out anymore.
 
 
The last admin step is that we need to go back to Site Settings and under the Search heading, click on the Result Types link.  Hover over our new Result Type until the dropdown menu appears and select the Edit function.
 
 
Once the page loads, we'll see all the settings from before when we copied the existing one, but now that we have our Item Display Template created we're going to update Search Results to use it.  Scroll down until you see the section Actions > 'What Should These Results Look Like?' and select the dropdown.  In there we should see our new "Excel Item - BP" listed alphabetically right around the original (that's why we updated the Title to find it).  Select this new item, then click the Save button.
 
Note:  Under the dropdown you'll see a disclaimer about the latest properties in your template will update if you make changes.  This applies to any new Managed Properties you add into your template.  See next paragraph on this.
 
 
The page will now post back to the Manage Result Types where you can review and verify that Excel results will now use our custom Item Display Template.  One thing to note from the below image is that every time you update your template and Publish a new Major Version, you have to come back here to do a Properties Sync (you won't see it initially following my instructions).  This forces SharePoint to push your latest template into the cache for users to see.  Otherwise you'll be beating your head on the keyboard trying to figure out why your new metadata isn't showing up!
 
 
Users Applaud your awesome-ness
Now that we've updated all Excel results to use our stellar new template, navigate to the page that you're showing Search Results on and verify your work.  We should see that now every Excel result has at least the Last Modified date showing as we don't have any conditional logic around it being there.  But if the result has that metadata attached to it, our new table will be built out with that metadata for our users.  Now you can sit back and bask in your rockstar-ness for making a small change with big impacts.
 
 
While this may not seem like a big thing for all the steps above, it can help your users find information they didn't know they might be looking for, and if you've set this value up as a Refiner they can find more excellent resources like this by filtering or searching by it.   The big return here is cutting down the time they spend searching for content and finding exactly what they needed right away.
 
 
 
References:
What’s new in Microsoft SharePoint Server 2013 - Support.Office.com
 
Introducing Search Schema for SharePoint 2013 - TechNet
 
Overview of the search schema in SharePoint Server 2013 - TechNet
 
Understand SharePoint Crawled and Managed properties for search - ShareGate Blog
 

by Craig Pilkenton via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment