List Item Ratings
How can I update a list item's rating either via csom or the rest api?
March 24th, 2015 3:58pm

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();

}

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 5:51pm

is this going to set the rating to 2? or will this update the item to include this new rating? What I mean is if the item has a rating of 4 by one person and then I update the item with a rating of 2, I would expect the item's rating to be 3.
March 24th, 2015 8:31pm

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);
            
            
        
}
Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 10:14pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics