SharePoint 2013 Discussion Board: Likes Count is Not Updated

I have been working often with Discussion Boards these days and have come across a number of interesting scenarios and issues. This article describes a peculiar problem one of our customers faced and how we helped them to resolve it.

Manpreet has provided a nice introduction to discussion boards here.

The customer reported that the likes count for discussion topics on a particular site was not getting updated.

We were investigating the issue from various perspectives and were not able to get any clue. We noticed that the problem was reproducible for newly created discussion boards on that specific site. It used to work as expected on the rest of the sites, we spent a lot of time analyzing the site columns, the content types for discussion boards. I even checked that if there is any issue with the JSLink or JavaScript for the discussion boards in the site, but did not get any clue.

Once, I clicked on the "Categories" list in the site just to check if there is any clue to the issue, and I was not able to get to the "All Items" view of the list. I tried using PowerShell to access the list and I was getting error there as well, same was the case with SharePoint Manager.

Then we focused on the categories list. Our solution strategy was to simply re-provision the categories list on the site and that was it! We managed to solve the issue by re-creating the categories list. Here is how we did that:
  1. Rename the existing, corrupt categories list to Categories_old and also change the URL.
  2. De-activate the "Community Site" feature and Re-activate it - this would re-create the "Categories" list on the site.
Following is a PowerShell script that does this.
  1. $web = Get-SPWeb <Affected web URL here>  
  2. $categorieslist = $web.Lists["Categories"]    
  3. $categorieslist.Title = "Categories_old"  
  4. $categorieslist.RootFolder.MoveTo($web.Url + "/lists/categories_old")  
  5. $categorieslist.Update()  
  6. Write-Host "Categories list issue fixed..."  
  7. Disable-SPFeature -Identity CommunitySite -Url $web.Url  
  8. Enable-SPFeature -Identity CommunitySite -Url $web.Url  
  9. Write-Host "Community Site Feature reactivated..."  
Now the likes count was getting reflected on the discussion when users liked the discussion topic or any replies to the discussion. Still, the likes count was not correct. The reason was, there were many likes done when the issue was there on site. 

The discussion boards have a couple of content types:

  1. Discussion

    A folder based content type, used as a container for the messages of the discussion. This represents the discussion topic and has a number of metadata properties for the discussion.

  2. Message

    An item based content type represents actual messages in the discussion and messages are stored in the folders of Discussion content type.
With this basic knowledge, we wrote another PowerShell  script that iterated through  all the folders in the list (Items of 'Discussion' content type) and summed up the value of the 'LikesCount' column of the folder item itself and that of all the items in that folder (Items of 'Message' content type) and finally set the value to the 'DescendantLikesCount' field of the folder. Confusing? let us look at the script, that would make matters easier to understand,
  1. $web = Get-SPWeb <Affected web url here>  
  2. $list = $web.Lists[<Affected list name here>]  
  3. foreach($discussion in $list.Folders)  
  4. {  
  5.     $query = new-object -TypeName Microsoft.SharePoint.SPQuery  
  6.     $query.Folder = $discussion.Folder  
  7.     $messages = $list.GetItems($query)  
  8.     $likesCount=$discussion.Item("LikesCount")  
  9.     foreach($message in $messages)  
  10.     {  
  11.        $likesCount=$likesCount+$message["LikesCount"]   
  12.     }  
  13.     $discussion["DescendantLikesCount"]=$likesCount  
  14.     $discussion.Update()  
  15. }  
  16. write-host "Likes Count Successfully Updated..." 

Up Next
    Ebook Download
    View all
    Learn
    View all