Rating an item of a list is one of the new features of SharePoint 2010. Lately I was asked how to add this feature programmatically to the Page list of a publishing site. First, I was expecting to find a sort of SPList.EnableRating property or something like that. There is no such a thing.
Via the UI, on the list settings page, you just have to click the Ratings settings link and then select YES. When you enable Ratings on a list, SharePoint adds two columns to the content type(s) you choose : AverageRatings and RatingCount.
Finally, after some readings and few searches, here is how I made it using PowerShell :
First, we have to activate a site scoped feature called Ratings with ID 915c240e-a6cc-49b8-8b2c-0bff8b553ed3 :
Enable-SPFeature -Identity Ratings -Url http://SiteCollectionUrl
Then, we add the two columns to the content type(s) of the list :
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $site = New-Object Microsoft.SharePoint.SPSite("
http://SiteCollectionUrl
") $web=$site.OpenWeb() if ($web -ne $Null) { $AverageRating=$web.Fields[[System.Guid]"5a14d1ab-1513-48c7-97b3-657a5ba6c742"] $RatingCount=$web.Fields[[System.Guid]"b1996002-9167-45e5-a4df-b2c41c6723c7"] $PagesList=$web.lists["Pages"] if ($PagesList -ne $Null) { Write-Host " Processing List : " $PagesList.Title $ct=$PagesList.ContentTypes["Page"] If ($ct -ne $null) { Write-Host " Adding Rating columns to content type : " $ct.name $AverageRatinglink = new-object Microsoft.SharePoint.SPFieldLink $AverageRating $RatingCountLink = new-object Microsoft.SharePoint.SPFieldLink $RatingCount $ct.FieldLinks.Add($AverageRatinglink) $ct.FieldLinks.Add($RatingCountLink) $ct.update() } } } $web.Dispose()
Hope this helps
Hi i am using the following command for sharepoint 2010 but it is giving me the following error:
ReplyDeleteNew-Object : Constructor not found. Cannot find an appropriate constructor for
type Microsoft.SharePoint.SPFieldLink
Please Help
Hi Ian,
ReplyDeleteDid you activate the 'ratings' feature as mentioned? If you did not please do. Otherwise, please post your code that I can help you diagnose the problem.
Regards.
I am vary interested in your post and that is very helpful and useful for me. I like it very much and that is more shareable. Thanks for share it with us.
ReplyDelete