Tuesday 25 April 2017

Showing Blog Post’s Comments and Likes Count in Content Search webpart

In SharePoint 2013 OnPremise, we had one requirement recently to show Comments and Likes count with each blog post in Content search webpart. We have two different Sharepoint Web Applications (different domains with different AD), and we were required to show this web part on both these applications front page. Also one important thing to note is that we have only one “My site” for both the applications.

Based on customer requirement, first we implemented this only for first web application.

During this implementation, we found that Likes count implementation was pretty straight forward (using “LikesCount” managed property, in display template, mapped to “ows_LikesCount” crawled property). The continuous or incremental crawl updates this property correctly (for continuous crawl it takes around 3 mins interval to update).

But we found that this wasn’t the case with Comments crawled property “ows_NumComments”. Though we created a managed property for it, say NumComments, it still wasn’t updating the comments counter after continuous or incremental crawl. It requires a Full crawl all the time to update the counter. We also found this issue mentioned here.

Comments counter (# Comments) column is not created as Site Column. It is local in Posts library.

To set it in Full Crawl, we can set “My Site” in new Content Source and set it to run every 3 or “x” mins.

But setting a Full crawl for updating only one field wasn’t our choice that time (considering performance overhead for whole application) so we decided to fetch Comments count with the help of REST calls from display template. For this to work with cross site (“My Site” was in a different domain), we had to add few entries in web.config of “My Site” to allow cross domain requests from single calling domain:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>



And put following entries inside the “customHeaders” tag:
<add name="Access-Control-Allow-Origin" value="<http://calling-domain>" />
<add name="Access-Control-Request-Method" value="GET, POST, HEAD, OPTIONS" />
<add name="Access-Control-Request-Headers" value="Content-Type, Authorization" />
<add name="Access-Control-Allow-Credentials" value="true" />

This worked great for only single domain for few months. But then there came the new request from Customer to add the same Content search web part on another application front page as well. Now for this cross site REST calls to work with 2 domains (or host-headers) we simply replaced value for “Access-Control-Allow-Origin” to “*” as mentioned in this MSDN blog

<add name="Access-Control-Allow-Origin" value="*" />

But after many unsuccessful attempts, It was concluded that “*” doesn’t work with SharePoint environment, it allows only single domain address for allowing cross site access.

So we decided to again give it a thought on our first approach of using Managed Property for Comments count for only this second application. As in this second application, blog posts are only coming from “My Site” so there was no need to do a Full crawl for the whole second application and it was needed for only “My Site”. So it was a considerable case for Full crawl as we have to do it only for “My Site” and not for those main sites with huge contents and libraries.

We created a new managed property “NumComments” for “ows_NumComments” crawled property and then created a new Content Source which had Full crawl scheduled to run for every ‘x’ minutes interval or so. We have assigned only “My Site” reference in new content source and scheduled to run it for every 30 mins. Earlier we thought it would be a performance overhead, but since we have to do full crawl for only “My Site”, it shouldn’t be a problem.

We kept the approach of using the REST call for first application as it is as there were posts from both the first application and “My Site” as well. So it was not convenient to allow Full crawl for first application because of huge contents.

Conclusion:

So we had to use this mixed approach for fulfilling our requirement (especially for showing Comments count) for both applications.

For First application, we used REST call approach for fetching comments count from Comments list (by adding above Access-Control-Allow-Origin entries in web.config in “My Site”, which works well for single domain)

For Second application, we used Full crawl technique (for only “My Site” set to full crawl) where 30 mins crawling schedule is set for updating NumComments managed property and then shows correct number in webpart.

I hope it will help someone struggling to overcome the similar situation.


Happy Learning! J

2 comments:

  1. nice your blog , information healt www.rumahsehatdenature.com

    ReplyDelete
  2. Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here.nocov

    ReplyDelete