The following CSOM code uses a Double to update the Rating column of a list:
public static void SetAverageRating() { ClientContext context = new ClientContext("http://basesmc15"); ListItem item = context.Web.Lists.GetByTitle("testlist").GetItemById(1); context.Load(item); context.ExecuteQuery(); item["AverageRating"] = 2.0; item.Update(); context.ExecuteQuery(); }
Yes you are correct the code overwrites the average which not what you want. You must use Microsoft.Office.Server.ReputationModel.Reputation.SetRating method contained in the Microsoft.SharePoint.Client.UserProfiles.dll. This method elevates permissions so it can modify hidden fields that track each user's ratings and then calculates the average. The code below works in my environment. Now what I saw was on items that did not have any rating there seemed to be a delay on when the rating showed up. This probably because SharePoint must do some provisioning of the associated hidden fields. The third argument is the item ID and the last argument is the rating.
public static void SetRating() { ClientContext context = new ClientContext("http://basesmc15"); List list = context.Web.Lists.GetByTitle("testlist"); context.Load(list); context.ExecuteQuery(); Microsoft.Office.Server.ReputationModel.Reputation.SetRating(context, list.Id.ToString(), 2, 4); }