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:
- Rename the existing, corrupt categories list to Categories_old and also change the URL.
- 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.
- $web = Get-SPWeb <Affected web URL here>
- $categorieslist = $web.Lists["Categories"]
- $categorieslist.Title = "Categories_old"
- $categorieslist.RootFolder.MoveTo($web.Url + "/lists/categories_old")
- $categorieslist.Update()
- Write-Host "Categories list issue fixed..."
- Disable-SPFeature -Identity CommunitySite -Url $web.Url
- Enable-SPFeature -Identity CommunitySite -Url $web.Url
- 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:
- 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.
- 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,
- $web = Get-SPWeb <Affected web url here>
- $list = $web.Lists[<Affected list name here>]
- foreach($discussion in $list.Folders)
- {
- $query = new-object -TypeName Microsoft.SharePoint.SPQuery
- $query.Folder = $discussion.Folder
- $messages = $list.GetItems($query)
- $likesCount=$discussion.Item("LikesCount")
- foreach($message in $messages)
- {
- $likesCount=$likesCount+$message["LikesCount"]
- }
- $discussion["DescendantLikesCount"]=$likesCount
- $discussion.Update()
- }
- write-host "Likes Count Successfully Updated..."