Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 17778

How To Use CSOM with Ratings in SharePoint 2013

$
0
0

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.


Viewing all articles
Browse latest Browse all 17778

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>