Showing document ID’s in search results

I’d like to quickly share how you can achieve this. Took me some time figuring it out, might as well help some other people. Note that I won’t be posting much background, just the basic on how to do it.

Read more on how to change search result display here: http://www.abelsolutions.com/totm/creating-customized-search-results-in-sharepoint-2013/

  1. Open SharePoint Designer of your site collection root.
  2. Go to All Files > _catalogs > masterpage > Display Templates > Search
  3. Copy Item_Default.html to Item_Custom.html (you can change that name of course). The corresponding .js is created automatically as well.
  4. Edit the net HTML file.
  5. Find the XML node called ManagedPropertyMapping and add the following:,’DocumentIdURL’:’dlcDocIdUrlOWSURLH’
  6. This will add the dlcDocIdUrlOWSURLH crawled property from SharePoint Search and add it to your results.
  7. Also, change the document head title tag so you can easily distinguish your custom template from those in SharePoint.
  8. Then change the JavaScript and HTML beneath to show the document ID wherever you want, for example:

For the JavaScript addition:

if(!$isNull(ctx.CurrentItem) && !$isNull(ctx.ClientControl)){
...
var docIdUrlArray = ctx.CurrentItem.dlcDocIdUrlOWSURLH.split(",");
var documentIdUrl = docIdUrlArray[0];
var documentId = docIdUrlArray[1];
...

And the HTML:

Document-id: <a href="_#= documentIdUrl =#_" title="Unique Link">_#= documentId =#_</a>
  1. Now save the file.
  2. Go to the page you want the search results to appear differently on (if you want to apply this globally, you should have changed Item_Default.html 🙂 ).
  3. Edit the Search Results Web Part, go to the section labeled “Display Templates” and select “Use a single template to display items”, then select your newly created template from he list.
  4. Apply the web part changes.
  5. Save the page.

This results in:

documentid-search


2 thoughts on “Showing document ID’s in search results

  1. Hi,

    I am lost as to where the Java and html need to go.

    Please could you post the full text from the display template?

    many thanks
    Nick

    1. Hi Nick,

      You need to find your way in the Item_Default.html.
      You’ll see that at the bottom of the file there’s the rendering HTML, you add the HTML for displaying the document ID there. That HTML uses an inline Javascript tag _#= documentId =#_.
      So before that you need to add some JavaScript to actually assign the variable documentId from the current item.

      I deliberately don’t post entire files for several reasons:
      1. May aim is to enlighten and educate rather than just give you copy/paste mindlessness 🙂
      2. I’m not disclosing entire files from work for our clients
      3. If my file is a bit older than yours or the other way around, you may be using code that won’t work yet for you or won’t work anymore, depending on the SharePoint version you’re on.

      So my advice: read through the post again, and analyze the Item_Default.html copy. Figure out what is happening, adjust what you think is needed. I’ve given you all the necessary pointers to get it working.

      Good luck, Nick!

Leave a comment