Resetting SharePoint 2010 social rating using PowerShell

As you probably know from variety of blogs, including my book, SharePoint 2010 allows you to turn on rating feature on your list items and documents so that your users can rate the content.

One of the things you will notice in a while is that SharePoint 2010 rating system is using URL, among other things, to record which item was rated by which user.

If you delete the item and upload a new item with the same name in the same library, you will see that your rating remains the same even though you have deleted the old item.

The PowerShell script below will allow you to delete the rating on all of the item inside http://intranet.contoso.com , which is my test Team Site. I also assume rating will be deleted from Shared Documents document library. Feel free to tweak things to match your particular scenario.

1. Create a new PowerShell script file, in my case called: CleanupRating.ps1

2. Add the following contents to the file:

Add-PSSnapin Microsoft.SharePoint.PowerShell
function ClearRating([Microsoft.SharePoint.SPWeb]$web)
{
$serviceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
$mySocialRatingManager =
New-Object Microsoft.Office.Server.SocialData.SocialRatingManager($serviceContext);
$list = $web.Lists["Shared Documents"];
for($i=0; $i -lt $list.Items.Count; $i++)
{
$item = $list.Items[$i];
$ratingCount = $mySocialRatingManager.GetCount($web.Url + "/" + $item.Url);
$ratings = $mySocialRatingManager.GetRatings($web.Url + "/" + $item.Url);
for($j=0; $j -lt $ratingCount; $j++)
{
$mySocialRatingManager.DeleteRating($ratings[$j]);
}
$mySocialRatingManager.PropagateRating($web.Url + "/" + $item.Url);
}
}
$site = spsite "http://intranet.contoso.com"
$site.allwebs | foreach-object {ClearRating $_ }

Above, we get a hold of the current site collection. Then we iterate through sub-sites on the site collection and get a hold of the Shared Documents library on each site. We go through each file inside the library and if any contain rating we delete the rating and propagate new count back to the list.

3. Adjust your URLs to match your scenario; save the file and run the script

For more on rating synchronization and working with ratings – check out my book.

Enjoy!

This entry was posted in sharepoint 2010 and tagged , , . Bookmark the permalink.

Comments are closed.