Enable ratings in SharePoint with PowerShell and CSOM
In document libraries it is possible to enable ratings, both likes and average score (1-5). In this article I will cover how to enable "Likes" on the Pages library in a publishing site.
When configuring libraries manually, this is enabled from the "Rating settings" in the library:
But when we follow the remote provisioning pattern to create new sites by using PowerShell and CSOM, there is currently no available function in the API for this. This pattern is the recommenced approach when working with SharePoint Online or SharePoint 2013/2016 without access to server side PowerShell or code.
To understand what is required to programmatic enable ratings I had to, as always with SharePoint, inspect the code behind with a reflection tool like DotPeek.
How to enable likes ratings
This show the procedure to enable ratings with Likes on a Pages library. To enable "Star rating" (1-5 average) this would be similar, but not covered in this post.
- Add the required fields to the library
FieldName Guid AverageRating 5a14d1ab-1513-48c7-97b3-657a5ba6c742 RatingCount b1996002-9167-45e5-a4df-b2c41c6723c7 RatedBy 4D64B067-08C3-43DC-A87B-8B8E01673313 Ratings 434F51FB-FFD2-4A0E-A03B-CA3131AC67BA LikesCount 6E4D832B-F610-41a8-B3E0-239608EFDA41 LikedBy 2CDCD5EB-846D-4f4d-9AAF-73E8E73C7312
- Add "LikesCount" to default view
- Add "Ratings_VotingExperience" to RootFolder's property bag
Use PowerShell to enable ratings
Note: This function requires the Office Dev PnP PowerShell library to be installed and loaded in the current PowerShell session. I recommend using this library for working with PowerShell and SharePoint (both Online and On-Premises).
Example: How to use this function with a site
Using this with subsites within the Site Collection would require some extension to the function, as the Fields always are loaded form the RootWeb, but the library reside in the subsite.
When we have run the "Enable-CustomLikesRatingsOnLibrary" function on the desired library, we can see that Likes now are available:
Using this function it is now possible to provision new sites with PowerShell and CSOM and enable likes rating on libraries.