You can use the PnP Sample to enable rating in your list.
var ratingEnabler = new RatingsEnabler(clientContext);
ratingEnabler.Enable("ListName", VotingExperience.Ratings);
The value can be set as VotingExperience.Ratings
or VotingExperience.Likes
Reference - Core.ListRatingSettings
Or you can use the below code. Here , the GUID is hardcoded and they remain the same across the tenant:
public static void EnableRating()
{
string siteUrl = "https://sitecollectionurl.sharepoint.com/sites/test";
string userName = "[email protected]";
string password = "password";
string listTitle = "List";
Guid RatingsFieldGuid_AverageRating = new Guid("5a14d1ab-1513-48c7-97b3-657a5ba6c742");
Guid RatingsFieldGuid_RatingCount = new Guid("b1996002-9167-45e5-a4df-b2c41c6723c7");
Guid RatingsFieldGuid_RatedBy = new Guid("4D64B067-08C3-43DC-A87B-8B8E01673313");
Guid RatingsFieldGuid_Ratings = new Guid("434F51FB-FFD2-4A0E-A03B-CA3131AC67BA");
Guid LikeFieldGuid_LikedBy = new Guid("2cdcd5eb-846d-4f4d-9aaf-73e8e73c7312");
Guid LikeFieldGuid_LikeCount = new Guid("6e4d832b-f610-41a8-b3e0-239608efda41");
using (var ctx = = new ClientContext(siteUrl))
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List filesLibrary = ctx.Web.GetListByTitle(listTitle);
ctx.Load(filesLibrary);
ctx.Load(filesLibrary);
ctx.ExecuteQuery();
ctx.Load(filesLibrary.RootFolder, p => p.Properties);
ctx.ExecuteQuery();
filesLibrary.RootFolder.Properties["Ratings_VotingExperience"] = "Likes";
filesLibrary.RootFolder.Update();
ctx.ExecuteQuery();
EnsureField(filesLibrary, RatingsFieldGuid_RatingCount, ctx);
EnsureField(filesLibrary, RatingsFieldGuid_RatedBy, ctx);
EnsureField(filesLibrary, RatingsFieldGuid_Ratings, ctx);
EnsureField(filesLibrary, RatingsFieldGuid_AverageRating, ctx);
EnsureField(filesLibrary, LikeFieldGuid_LikedBy, ctx);
EnsureField(filesLibrary, LikeFieldGuid_LikeCount, ctx);
filesLibrary.Update();
ctx.ExecuteQuery();
ctx.Load(filesLibrary, view => view.DefaultView);
ctx.ExecuteQuery();
var defaultView = filesLibrary.DefaultView;
defaultView.ViewFields.Add("LikesCount");
defaultView.Update();
ctx.ExecuteQuery();
}
}
private static Field EnsureField(List list, Guid fieldId, ClientContext context)
{
FieldCollection fields = list.Fields;
FieldCollection availableFields = list.ParentWeb.AvailableFields;
Field field = availableFields.GetById(fieldId);
context.Load(fields);
context.Load(field, p => p.SchemaXmlWithResourceTokens, p => p.Id, p => p.InternalName, p => p.StaticName);
context.ExecuteQuery();
if (!fields.Any(p => p.Id == fieldId))
{
var newField = fields.AddFieldAsXml(field.SchemaXmlWithResourceTokens, false, AddFieldOptions.AddFieldInternalNameHint | AddFieldOptions.AddToAllContentTypes);
return newField;
}
return field;
}
Enable Ratings/Likes For A List In SharePoint Office 365 Programmatically Using C#
Note - in case there is some issue with rating not getting enabled, you can replace the property bag key as Ratings_x005f_VotingExperience
and then check. See the link in the comments.