SharePoint Online: How to Enable Rating in List using PowerShell?

Requirement: Enable Rating in SharePoint Online List

How to Enable Rating in SharePoint Online?
Rating feature in SharePoint Online helps to highlight documents and list items. Rating can be enabled in SharePoint Online list and libraries to let users to see and rate items. Rating can be configured as either star ratings or as Likes.

  1. Star Ratings
    When a list is configured for star ratings, Every time someone rates the item or a document, the scale displays the average rating of all those who rated it. To rate an item within a list, click on the star that represents the rating you want to give the item. Once you select the rating, the item is updated to include your rating. If you rate the item again, the previous rating applied is replaced by the newly selected rating.

  2. Like Ratings
    Likes are similar to what you use on Facebook, Twitter, and LinkedIn, etc. When ratings type is set to "Likes", items are presented with Like button (little heart icon) which includes the number of Likes an item has received. You can Like an item, if you have not yet done so, or Unlike the item, if you have already Liked. You can record your like/unlike by clicking the Like icon.

To enable ratings, we must activate it under list settings. To activate them, do the following:
  1. Navigate to the SharePoint Online list or library where you want to activate ratings.>> Click on List Settings
  2. On the List Settings page, Click on the "Rating Settings" link under General Settings section.
  3. On the Rating Settings, Set "Allow items in this list to be rated" option to "Yes"
  4. For the "Which voting/rating experience you would like to enable for this list" option, select either Star Ratings or Like.rating settings in sharepoint online
  5. Click the OK button to save the changes.

The ratings for the list will be enabled.
sharepoint online enable rating


PowerShell to Enable Rating Settings in SharePoint Online:
Configuring rating settings in SharePoint Online list or library is bit tricky as there is no direct method to set ratings. We need to perform below steps:
  1. Add Rating site columns to the list
  2. Add Rating Fields to Default view
  3. Set List Root Folder's Property bag value.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Add Fields to List 
Function Add-SPOSiteColumnToList([Microsoft.SharePoint.Client.List]$List, [GUID]$FieldID)
{
    #Get Fields from List
    $Ctx.Load($List.Fields)
    $Ctx.ExecuteQuery()
    
    #Get the Site Column from Web
    $SiteColumn = $List.ParentWeb.AvailableFields.GetById($FieldID)
    $Ctx.Load($SiteColumn)
    $Ctx.ExecuteQuery()

    #Check if the Field exist in list
    $ListField = $List.Fields | where {$_.ID -eq $SiteColumn.Id}
    if($ListField -eq $NULL)
    {
        #Add the site column to the list
        $NewColumn = $List.Fields.Add($SiteColumn)
        $ctx.ExecuteQuery()
        Write-host "Site Column '$($SiteColumn.Title)' Added to the List Successfully!" -f Green
    }
}

Function Set-SPOListRatingSettings([string]$SiteURL, [string]$ListName, [string]$RatingsType)
{
    #Setup Credentials to connect
    $Cred = Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Rating Site Columns
    $AverageRatingFieldID = [guid]"5a14d1ab-1513-48c7-97b3-657a5ba6c742"
    $RatingCountFieldID = [guid]"b1996002-9167-45e5-a4df-b2c41c6723c7"
    $RatedByFieldID = [guid]"4D64B067-08C3-43DC-A87B-8B8E01673313"
    $RatingsFieldID = [guid]"434F51FB-FFD2-4A0E-A03B-CA3131AC67BA"
    $LikesCountFieldID = [guid]"6E4D832B-F610-41a8-B3E0-239608EFDA41"
    $LikedByFieldID = [guid]"2CDCD5EB-846D-4f4d-9AAF-73E8E73C7312"

    #Call the function to Add Rating Site columns to the List
    Add-SPOSiteColumnToList -List $List -FieldID $AverageRatingFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatingCountFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatedByFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $RatingsFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $LikesCountFieldID
    Add-SPOSiteColumnToList -List $List -FieldID $LikedByFieldID

    #Update Field to Default view
    $ListDefaltView = $List.DefaultView
    $ViewFields = $ListDefaltView.ViewFields
    $Ctx.Load($ListDefaltView)
    $Ctx.Load($ViewFields)
    $Ctx.ExecuteQuery()

    If($RatingsType -eq "Ratings")
    {
        #Add Ratings Fields to default view
        If($ViewFields -notcontains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Add("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Ratings Field Added to the Default View!"
        }
        #Remove Likes Field from Default View
        If($ViewFields -contains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Remove("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Removed from the Default View!"
        }
    }
    ElseIf ($RatingsType -eq "Likes")
    {
        #Add Likes Fields to default view
        If($ViewFields -notcontains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Add("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Added to the Default View!"
        }
        #Remove Ratings Field from Default View
        If($ViewFields -contains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Remove("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Rating Field Removed from the Default View!"
        }
    }
    Else #Disable Rating!
    {
        #Remove Ratings Field from Default View
        If($ViewFields -contains "AverageRating")
        {        
            $ListDefaltView.ViewFields.Remove("AverageRating")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Rating Field Removed from the Default View!"
        }
        #Remove Likes Field from Default View
        If($ViewFields -contains "LikesCount")
        {        
            $ListDefaltView.ViewFields.Remove("LikesCount")
            $ListDefaltView.Update()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Likes Field Removed from the Default View!"
        }
    }

    #Set Rating Setting for the List
    $RootFolder = $List.RootFolder
    $RootFolderProperties = $RootFolder.Properties
    $Ctx.Load($RootFolder)
    $Ctx.Load($RootFolderProperties)
    $RootFolderProperties["Ratings_VotingExperience"] = $RatingsType
    $RootFolder.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Rating Settings Updated for the List!"
}
#Set Variables
$SiteURL= "https://crecent.sharepoint.com/sites/Marketing"
$ListName="Team Documents"
$RatingsType = "Ratings" #Ratings or Likes or "" (To Disable!)

#Call the function to set rating settings in list
Set-SPOListRatingSettings -SiteURL $SiteURL -ListName $ListName -RatingsType $RatingsType 
SharePoint Online: How to Enable Rating in List using PowerShell? SharePoint Online: How to Enable Rating in List using PowerShell? Reviewed by Salaudeen Rajack on August 17, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.