I needed to change the delete button on the ribbon on some specific App parts on SharePoint 2013, so looking around I found a post that shows how to trigger an event with each ribbon change by Dennis George which was of great help because it did exactly what I needed, to capture when a certain tab was selected. The issue? It doesn’t work on SharePoint 2013 because the CUI.Ribbon.prototype.$L is always undefined, so I started debugging the whole ribbon and found that to make the code work in SharePoint 2013 I just had to change $L with $1q_0 and that was it. So here’s the code to trigger an event each time there is a change on the SharePoint 2013 ribbon. Use it at will:

I’ve been struggling with this way too long… But in the end and after a whole lot of googling I was able to make this work as magic (that is with JavaScript/jQuery). What you’ll need to do is create a new Display Template for your search, add some managed properties to it and put some magical JS code. I will assume you know how to create a new Display Template, but if not you can search on google for it or you can read this blog series by Microsoft’s own Bella Engen. So let’s get started.

Display Template

For your display template you’ll have to add two managed properties. The ones that you will be needing are the List ID and the List Item ID, you can add LikesCount as well but the main problem with it is that if you show that property it wouldn’t be updated until your next crawl which kind of ruins the whole thing since if you just unliked the item you might want that to show on other user’s searches as well, so retrieving that value in real-time with JavaScript would be your best bet. Back to business, in my case those properties where already there and mapped (I think they might be by default), but if in your case the managed properties are not created go ahead and create them. Here’s how those properties looked:

Look at the ListID and ListItemID Managed Properties

So from there you have to add those two properties to the Display Template as follows:

As you can see on that line I also left the LikesCount added, but you don’t really need it. After having that let’s leave the Display Template for a second and move on to…

The JavaScript

I’ve made this on a separate JS file which I’ve added to the Search Results page. Here I’ve made several functions to handle the likes. The first one would be the click for the “Like/Unlike” link, but you wouldn’t understand that function until you see the one that goes and checks if the user had already liked the item, for this I have to thank Simon Pedersen for his post where he has a similar function to the one I was working on (and yes, at this point I’m wondering if I’m a programmer or just a good googler). So anyway, here’s the function I’ve made:

At the end this function does a lot of things, some are self-explanatory, but some I need to explain you. What I’ve decided to do is to set a data attribute on the element that holds the number of likes so that I can get the value from there whenever and wherever I need it, that’s done by the

span.data('userliked',didUserLiked);

line, and then to get that value you just need to do the following

span.data('userliked');

and be done with it. The other thing I think I need to explain is that I’m changing the value of the Text for the like/unlike here instead of doing it from the Display Template because it needs to be done asynchronously and as the display template goes item by item it won’t wait until you get the result, so this will be updated via JavaScript on my end.

Now I can show you the other two functions which could be just one, but I just felt like splitting them…

Let’s go back to the Display Template

Now we need to do make this whole thing work… So we need to call the functions when needed and pass them the values of the ListID and ListItemID managed properties. So let’s start with the JavaScript part of the display template:

This will get you the ListID and ListItemID properties on two handy javascript variables and if they are not null they will be sent to the getUserLikedPage from the separate JS file. Then we need to change the HTML part so that you can display the things that you want displayed, here’s that code:

As you can see I’ve also added the ListID and ListItemID properties to the span as attributes to be able to find that easily with my jQuery.

With this code you should be getting your likes on the search results and being able to Like or Unlike from those same results (if you have set the Display Template correctly and activated the Rating features of the list).

And here’s how the end result looks like on my end:

If you have any questions feel free to ask and I’ll try to answer them below.

In the project I’m working on they asked me to create a JS file that would change the Result Sources on the fly as the user clicked on the navigation links. So after googling a bit I’ve stumbled upon Elio Struyf’s post where he changes the Result Sources based on the query, so that pointed me in the right direction… At the beginning my idea was to make it so that it changed the Result Source based on a parameter in the query string, so I thought that Elio’s code would be a great base and that I just needed to make the changes so that it would pull the values from the QueryString instead of the query for the search. Right?

Wrong. That approach turned out to be a bit more difficult than expected. To start with I learned that the SharePoint site navigation didn’t account for query parameters so I decided that I would change the href of the links via the JavaScript file that I was writing, that was easy enough… But still the code was not executing, after a careful look at the situation I realized that the fillKeywordQuery function was not being called. The problem was that in the project we don’t use a default Search Box Web Part, we use a simple web part that fills the results onto a customized Search Results Web Part so the function was never being called. I added a default Search Box Web Part and noted that when I clicked on the search image the code was executing just fine.

In the end I decided to go with a different approach that doesn’t depends on the QueryString, it just changes the behavior of the fillKeywordQuery each time you click on the navigation links.

So the first thing that I had to do was to remove the href and the onclick from all the links so that they wouldn’t redirect me to the page that was assigned on the SharePoint side. To do that I used just some simple jQuery:

In my case I also needed to call the fillKeywordQuery myself since the web part that we’re using doesn’t call it. If that is your case you’ll also need to set a KeywordQuery and get the DataProvider and call the function on each of the clicks from the links.