Second Life of a Hungarian SharePoint Geek

February 16, 2011

How we can set rating in the name of other users from code?

Filed under: Rating, Social computing, SP 2010 — Tags: , , — Peter Holpar @ 22:47

Two months ago I wrote about my unsuccessful attempts trying to impersonate users in call into the SharePoint 2010 social API. At that time I was not able to show you the way to achieve this, and asked the help of the readers to point to the right direction.

Fortunately, Andres Ciampo wrote a comment to that post including a code snippet that seems to do the job of the impersonation.

The trick in this solution is to create a custom, impersonated HttpContext, that is set as the current context and used to create the SPServiceContext that is passed to the SocialRatingManager constructor, as illustrated in this code:

  1. private void SetRatingForListItem(SPListItem listItem, int ratingValue, String ratingTitle, SPUser user)
  2. {
  4.     IPrincipal impersonationPrincipal = new WindowsPrincipal(new WindowsIdentity(GetUpn(user)));
  6.     HttpRequest request =
  7.     new HttpRequest(string.Empty, listItem.Web.Url, string.Empty);
  9.     HttpResponse response =
  10.     new HttpResponse(
  11.     new System.IO.StreamWriter(new System.IO.MemoryStream()));
  13.     HttpContext impersonatedContext =
  14.     new HttpContext(request, response);
  16.     impersonatedContext.User = impersonationPrincipal;
  17.     impersonatedContext.Items["HttpHandlerSPWeb"] = listItem.Web;
  19.     HttpContext.Current = impersonatedContext;
  21.     SPServiceContext serviceContext = SPServiceContext.GetContext(impersonatedContext);
  22.     Uri uri = new Uri(String.Format("{0}/{1}", listItem.Web.Url, listItem.Url));
  24.     SocialRatingManager ratingManager = new SocialRatingManager(serviceContext);
  25.     ratingManager.SetRating(uri, ratingValue, ratingTitle);
  26.     ratingManager.PropagateRating(uri);
  28. }

I verified the result using the GetRatingsAndCommentsForListItem method (see my former post for its code) and it proves the rating was really set in the name of the impersonated user.

Andres, thanks again for the idea and sharing the code with us!

1 Comment »

  1. Hi, This was an excellent article and it helped me out. But I am facing an issue on calling the PropagateRating(URI) Method. The exception I am getting is “Object Reference not set to an instance”. And the second thing, the list item is not getting reflected immediately. It is taking an hour because of the Timer job – Social Rating Syncronizing needs to run. Is there any way to make this reflecting immediately. I guess, PropagateRating() method helps this. But I am not able to execute that. Your help would be very much appreciated. Thanks in advance.

    Comment by Sathish — May 10, 2013 @ 11:51

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: