Share-n-dipity

SharePoint serendipity is the effect by which one accidentally discovers something fortunate, especially while looking for something else entirely. In this case, it is the occassional musings, observations, and Ouija board readings about the phabulously

How To Use CSOM with Ratings in SharePoint 2013

How To Use CSOM with Ratings in SharePoint 2013

  • Comments 3
  • Likes

I wrote a related post today about how to find the fields names in a list:  http://blogs.technet.com/b/speschka/archive/2013/07/08/how-to-quickly-and-easily-get-a-list-of-fields-in-a-sharepoint-2013-list.aspx.  The reason I wrote that was because it came in handy when I was trying to use CSOM to manage ratings on list items.  What I found is that there's a nice little API to add ratings, but not one to get ratings.  Here's a quick overview:

To set ratings, the first thing you need to do is have a reference to Microsoft.SharePoint.Client.UserProfiles.  In there you will find a static class called Reputation that allows you add a rating to an individual item.  The code to apply it them looks relatively simple:

 

using (ClientContext ctx = new ClientContext(https://yourSiteUrl))
{
                    Web w = ctx.Web;
                    List l = w.Lists.GetByTitle("yourListName");
                    ctx.Load(l, info => info.Id);
                    ctx.ExecuteQuery();

 

                    string ListID = l.Id.ToString();
                    Microsoft.Office.Server.ReputationModel.Reputation.SetRating(ctx, ListID, 1, 5);
                    ctx.ExecuteQuery();
}

In this example I hard-coded the rating to apply to the list item with the id of 1, and I gave it a rating of 5.  Pretty simple, yeah?  Cool.  Now getting the ratings for a list item was a little more complicated.  There isn't good OM code to do it, so you have to finesse it a bit.  Here's what it looks like:

using (ClientContext ctx = new ClientContext("https://yourSiteUrl"))
{
                    Web w = ctx.Web;
                    List l = w.Lists.GetByTitle("yourListName");

                    CamlQuery cq = CamlQuery.CreateAllItemsQuery();
                    ListItemCollection lic = l.GetItems(cq);

                    ctx.Load(lic, items =>
                        items.IncludeWithDefaultProperties(
                        item => item.Id,
                        item => item["AverageRating"],
                        item => item["RatingCount"],
                        item => item["Ratings"]));

                    ctx.ExecuteQuery();

                    foreach (ListItem li in lic)
                    {
                        Debug.WriteLine(li["AverageRating"]);
                        Debug.WriteLine(li["RatingCount"]);
                        Debug.WriteLine(li["Ratings"]);
                    }
}

Really the only interesting thing I think there is just that you have to manually plug in the list of the hidden fields that SharePoint uses to track the rating information.  Using this same approach, you can also grab Likes information as well - just use the post I linked to above to get the appropriate field names.

Comments
  • Does this Microsoft.Office.Server.ReputationModel.Reputation.SetRating method work for Claims aware Sharepoint site and also does this CSOM snippet work with SP 2013 Proivder App ? We are getting the exception " The assembly Microsoft.SharePoint.Portal.Proxy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not support app authentication" when used with High trust app on ADFS claims aware web app

  • Same exception for me in Provider Hosted App, testing directly from Visual Studio:
    "The assembly Microsoft.SharePoint.Portal.Proxy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not support app authentication."

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment