Last week, I posted on how to query items matching a hashtag. Today, I want to extend that concept further and talk about how to determine which tags are trending. What I am specifically talking about today is the functionality in the “Trending #tags” section of the Newsfeed page.
You can see the list there on the bottom right. It lists each tag and provides the number of times it was used in the last week. Once you know how to query for this information, it’s actually quite simple. However, it took quite a bit of digging for me today to figure it out. How did I do it? Well I knew it was search based, so I wanted to capture the query that the built-in web part was using. To do this, I went back to my on-premises environment and changed the diagnostic logging settings under SharePoint Server Search –> Query to Verbose.
I then started a trace with ULS Viewer and went to the Newsfeed page to see what was going on. After some digging, I noticed a query as highlighted below.
That query specifically was something like this:
ContentTypeId:0x01FD* write>="2013-11-06 21:46:30Z" -ContentClass=urn:content-class:SPSPeople
Specifically this query is looking for specific content types (you’ll see which ones shortly) and it uses the write keyword to get everything that has happened in the last week. Lastly, it excludes people results. Interesting, so what does it look like when I paste the query into my Search Center?
Interesting. Those don’t look like trending tags at all! In fact, what we are looking at are posts and replies fro the social feed. Interesting. Take a look at that Tags refiner on the left though. Those tags look familiar! Going back and looking at what the out-of-the-box web part, it turns out that they match and they are even in the same order. Could it be?
Edit the refinement panel and take a look at the refiners in use and you’ll see that the values in refiners match exactly to what the out-of-the-box web part gives us.
This gives us what we need to work with as a developer. If you wanted to use it with REST, you would use a query like this:
https://server/_api/search/query?querytext='ContentTypeId:0x01FD* write>="2013-11-06 21:46:30Z" -ContentClass=urn:content-class:SPSPeople'&refiners='Tags'
Here we also need to specify that we want it to give us a refiner using the refiners query string parameter. Specify a value of Tags and make sure it is enclosed in single quotes. You’ll notice in the Refiners element, we get each the name of each trending tag in convenient SharePoint Term form with the GUID as well as the RefinementCount.
From here, you should be able to manipulate this data to display however you want. You can also tweak the query now to exclude or include other entries as desired. One thing to note here is that this means that trending tags is solely based off of the newsfeed. Tagging documents with an associated enterprise keyword as I mentioned in my previous blog post won’t affect what is trending.