Second Life of a Hungarian SharePoint Geek

December 6, 2010

Rate an item from code

Filed under: Rating, Social computing, SP 2010 — Tags: , , — Peter Holpar @ 00:16

In a post about a year ago I promised that I would show in my next post how to rate an item from code.

Similar to the case where we retrieved ratings in the former post, we should use the SocialRatingManager class, but instead of the GetRatings method, we use the SetRating method.

The following code snippet shows a sample for its usage, where calling SetRating sets the rating for the item, while PropagateRating triggers a recalculation of the average social rating for the specified URI:

  1. private void SetRatingForListItem(SPListItem listItem, int ratingValue, String ratingTitle)
  2. {
  3.     SPServiceContext serviceContext = SPServiceContext.GetContext(listItem.ParentList.ParentWeb.Site);
  4.     Uri uri = new Uri(String.Format("{0}/{1}", listItem.Web.Url, listItem.Url));
  5.  
  6.     SocialRatingManager ratingManager = new SocialRatingManager(serviceContext);
  7.     ratingManager.SetRating(uri, ratingValue, ratingTitle);
  8.     ratingManager.PropagateRating(uri);
  9. }

And here is an example for the call of the sample SetRatingForListItem method, that adds a five-star rating and a rating title to the first item of a list (of course, rating must be enabled for this list!):

  1. String url = "http://sp2010";
  2. using (SPSite site = new SPSite(url))
  3. {
  4.     using (SPWeb web = site.OpenWeb())
  5.     {
  6.         SPList list = web.Lists["YourList"];
  7.         SPListItem listItem = list.Items[0];
  8.  
  9.         SetRatingForListItem(listItem, 5, "Very good!");
  10.     }
  11. }

If you try the example, you might be surprised (or disappointed) as there are no changes shown for the item in the list.

The reason for this behavior is that when we set the rating, it is change the value in the Social Database, and the list item is displayed with the rating from the the Content Database.

So we have to wait until data is synchronized from the Social Database to the Content Database. This task is performed by the User Profile Service Application – Social Rating Synchronization Job (as its description says: Timer Job to synchronize rating values between Social Database and Content database). The job is scheduled to run hourly by default. If you can’t wait, you can start the job immediately either from the UI or from code, as described in this post.

image

In this post we set the rating of the item on behalf of ourselves. In the next part (for which I hope you should not wait another year) we will see how we can (or how we can’t, to be exact) set rating in the name of other users.

3 Comments »

  1. […] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Rate an item from code […]

    Pingback by How we can’t set rating in the name of other users from code? « Second Life of a Hungarian SharePoint Geek — December 16, 2010 @ 01:32

  2. Hi, Firstly thank you for your post. I am using the exact same code snippets but I don’t seem to get it work. Once I call .SetRating method I get “User Profile cannot be update” error. Any ideas are much apppreaciated?

    Thanks,
    Shiv

    Comment by Shiv — June 28, 2012 @ 15:21

  3. Hi Peter, Great post. very helpful.
    After reading your posts I was able to build a solution using the Social Data Services web service. And it works fine except for a tiny little bit: The PropagateRating() doesn’t seems to work for users with Read Only permissions, but works fine for others. Mind that Read Only users can rate and it gets reflected, but without the Propagation, I need to wait for the timer job.

    Do you know, if there’s a reason for this? Have you got any suggestions I can use to get it working for Read Only users?

    Thanks,
    Rizi

    Comment by Rizi — March 2, 2013 @ 19:17


RSS feed for comments on this post. TrackBack URI

Leave a reply to How we can’t set rating in the name of other users from code? « Second Life of a Hungarian SharePoint Geek Cancel reply

Create a free website or blog at WordPress.com.