Thursday, March 26, 2015

Display Comments and Likes in content search webpart

[SOLVED] Instead of relaying on the search crawler for the #Comments field, I simply added the following line in the Display Template and that works with continous crawling:
_#= ctx.CurrentItem.NumComments =#_
[/SOLVED]
 
I want to show both number of Likes and Comments in the News webpart, which I have built using Content Search and my own custom Display Template.

 
The Likes is easy, it already exists. Just select “LikesCount” in the Property Mappings List (Note: I have added own lines in the display template, hence the extra rows below):

The number of comments are not included though, so that property needs to be created in the Search Schema. In SharePoint 2010 and in a Content Query Web Part, I referred to this field with #Comments. But that does not work in 2013.

Go to Search Application on CA, and to Search Schema. Add a new Managed Property, I called it just “NumComments” and map it to the column “ows_NumComments”. Configure it like this:
In the display template, I have used Glyphicons to display a Like button and a Comment button. This is my code for the “Like and comment” part of the news (I have removed the <> from code and replaced with [] ):

[div class="cbs-largePictureLineCustom ms-noWrap"]
[span title="_#= $htmlEncode(line4) =#_" id="_#= line4Id =#_"]_#= likesCount =#_  [/span]
[span class="glyphicon glyphicon-thumbs-up"][/span]
[span title="_#= $htmlEncode(line5) =#_" id="_#= line5Id =#_"]_#= line5 =#_ [/span span class="glyphicon glyphicon-comment"][/span]
[/div]

When running continuous crawling, the Likes showed up within a 3 minute interval (I have set that interval using Powershell cmdlet Get-SPEnterpriseSearchServiceApplication and continouscrawl), but the Comments never showed up unless I was doing a Full Crawl.
So to enable the number of comments in the continuous crawl, click on the “ows_NumComments” link that is displayed in the Search Schema:

And deselect the “Include in full-text index” (this turns out to not help anyway which you will find out below):

But the Comments still did not show up in the web part, (unless I did a Full Crawl).
So I did a Reset Index. Ran a Full Crawl, to make sure that my changes above had been implemented. Still new comments are not displayed unless I run a Full Crawl.
Looked in the Search Schema again, but this time I clicked on Crawled Properties. Found a difference between the field ows_Comments and ows_LikesCount. The later had a mapping, which ows_Comments missed. So I added the following mapping into the "ows_NumComments":

Started a Full Crawl again to implement the changes.
Added a new comment and waited for the continuous crawl/incremental crawl to pick up the changes, but it does not show in the content search web part. Only when I do a full crawl. Don’t know if it has to do with the NumComments field being a Lookup field? If you look at the column properties on the blog site (the News are built on a blog site template) then this is all the info you get:

The column is not created as a Site Column, but a local in the Posts library of a blog site.  The only solution I could think of, and I don’t like it but what to do, is to create a new Content Source with only the link to the comments in it and set that to Full Crawl each 3 minutes. Don’t know if this will work in the long run, but for now it’s good enough.

 

4 comments:

Anonymous said...

Hi Lise,

Could you give a bit more information what have you done to achieve this? DId you use CSWP or CQWP? I have used CSWP and managed to get the Number of Likes but not number of comments,As i am very new to SharePoint

Thanks

Lise Rasmussen said...

Hi!

I used the CQWP at the end, instead of the CSWP. Please read my full solution here:
http://sharepointbabe.blogspot.se/2015/04/display-comments-and-likes-in-cqwp.html

Thank you for visiting my blog :)

Cindy Dy said...


I like the way on how you put up your blogs. Wonderful and awesome. Hope to read more post from you in the future. Goodluck. Happy blogging!

Bubble
www.gofastek.com

Silvia Jacinto said...


Mistake does not only teach you how to be strong but also how to stand up again and to do good next time. Visit my site for more information. Have a good day always and thank you for sharing your knowledge and wonderful article to the public. I do hope that you can inspire more readers.

n8fan.net

www.n8fan.net