Monday, October 17, 2011

SharePoint 2010 : How to add rating to a list using PowerShell

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 :

 $site = New-Object Microsoft.SharePoint.SPSite("http://SiteCollectionUrl")  
 if ($web -ne $Null)  
       if ($PagesList -ne $Null)  
             Write-Host " Processing List : " $PagesList.Title  
             If ($ct -ne $null)  
                   Write-Host " Adding Rating columns to content type : " $  
                   $AverageRatinglink = new-object Microsoft.SharePoint.SPFieldLink $AverageRating  
                   $RatingCountLink = new-object Microsoft.SharePoint.SPFieldLink $RatingCount  

Hope this helps