Enable Rating on sharepoint list by powershell

09/06/2015 10:28

To enable rating functionality on the sharepoint list, script adds two sharepoint rating columns to the list content type and view. However, if you check list settings/rating settings in UI, Enable Rating settings is still set to No, until you click the star icon in any list item. Then suddenly is Enable Rating set to Yes


Add-PSSnapin microsoft.sharepoint.powershell

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$webUrl = "https://yourSiteUrl"
$listName = "Your list name";

#enable feature if not enabled
#Enable-SPFeature -Identity Ratings -Url $webUrl

$web = get-spweb $webUrl
$list = $web.Lists[$listName]
$list.EnableVersioning = $true;
$list.Update()

 if ($web -ne $Null) 
 { 
       $averageRatingField = $web.Fields.TryGetFieldByStaticName("AverageRating");
       $ratingCountField = $web.Fields.TryGetFieldByStaticName("RatingCount");

       if ($list-ne $Null) 
       { 
             Write-Host " Processing List : " $list.Title 
             $ct = $list.ContentTypes[0] 
             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()               
                   $list.Update();

                   $view = $list.DefaultView;
                   $view.ViewFields.Add("AverageRating");
                   $view.ViewFields.Add("RatingCount");
                   $view.Update();
             }
             $list.Update(); 
       }
       $web.Update(); 
 }

 $web.Dispose()