Second Life of a Hungarian SharePoint Geek

January 14, 2010

Getting ratings and notes of users and items

Filed under: Rating, Social computing, SP 2010 — Tags: , , — Peter Holpar @ 17:40

Rating of items and creating notes are part of the new social features of SharePoint 2010. The following code snippets demonstrate some basic tasks, like displaying ratings and notes by user or by URL.

A few words about vocabulary. Notes are called comments on the object library level, for example the SocialComment class describes the properties of the note, although most of the class properties are derived from its parent class SocialData. The same is true for the SocialRating and SocialTag classes. We don’t use this latest one in the current post.

You should reference the Microsoft.Office.Server.dll and the Microsoft.Office.Server.UserProfiles.dll, and of course the mandatory  Microsoft.SharePoint.dll assemblies in your project, and include the following using directives:

  1. using Microsoft.SharePoint;
  2. using Microsoft.Office.Server.SocialData;
  3. using Microsoft.Office.Server.UserProfiles;

The GetRatingsAndCommentsForUrl method displays the rating and notes for the specified URL. Since our code  runs as a console application, the first we do is to get a reference for an SPServiceContext using the SPSite of the specified URL. We use the SPServiceContext instance to get the related SocialRatingManager and SocialCommentManager objects. SocialRatingManager has a GetRatings(Uri url) method, and SocialCommentManager has a GetComments(Uri url) method. These methods return a SocialRating and a SocialComment array respectively. We enumerates on the items of these arrays and display the information we need.

  1. private void GetRatingsAndCommentsForUrl(String url)
  2. {
  3.     using (SPSite site = new SPSite(url))
  4.     {
  5.         SPServiceContext serviceContext = SPServiceContext.GetContext(site);
  6.         Uri uri = new Uri(url);
  7.  
  8.         Console.WriteLine("Ratings of {0}", url);
  9.  
  10.         SocialRatingManager ratingManager = new SocialRatingManager(serviceContext);
  11.         SocialRating[] ratings = ratingManager.GetRatings(uri);
  12.         foreach (SocialRating rating in ratings)
  13.         {
  14.             Console.WriteLine("Rated by user: ‘{0}’, rating title: ‘{1}’, rating: ‘{2}’, rating date: ‘{3}’", rating.Owner.DisplayName, rating.Title, rating.Rating, rating.LastModifiedTime);
  15.         }
  16.  
  17.         Console.WriteLine("Comments of {0}", url);
  18.  
  19.         SocialCommentManager commentManager = new SocialCommentManager(serviceContext);
  20.         SocialComment[] comments = commentManager.GetComments(uri);
  21.         foreach (SocialComment comment in comments)
  22.         {
  23.             Console.WriteLine("Commented by user: ‘{0}’, comment title: ‘{1}’, comment: ‘{2}’, comment date: ‘{3}’", comment.Owner.DisplayName, comment.Title, comment.Comment, comment.LastModifiedTime);
  24.         }
  25.  
  26.     }
  27. }

The GetRatingsAndCommentsForListItem simply calls the previously defined GetRatingsAndCommentsForUrl method using the URL of the item it receives as parameter.

  1. private void GetRatingsAndCommentsForListItem(SPListItem listItem)
  2. {
  3.     GetRatingsAndCommentsForUrl(String.Format("{0}/{1}", listItem.Web.Url, listItem.Url));
  4. }

The GetRatingsAndCommentsForUser method expects a parameter of type SPUser and display all of the ratings and notes of the user. After getting the server context, we try to get the UserProfile for the user through the UserProfileManager class. The method is similar to the GetRatingsAndCommentsForUrl method, but in this case we use the GetRatings(UserProfile user) method of the SocialRatingManager and the GetComments(UserProfile user) method of the SocialCommentManager.

  1. private void GetRatingsAndCommentsForUser(SPUser user)
  2. {
  3.     SPSite site = user.ParentWeb.Site;
  4.     SPServiceContext serviceContext = SPServiceContext.GetContext(site);
  5.  
  6.     UserProfileManager upm = new UserProfileManager(serviceContext);
  7.     if (upm.UserExists(user.LoginName))
  8.     {
  9.         UserProfile profile = upm.GetUserProfile(user.LoginName);
  10.  
  11.         Console.WriteLine("Ratings of {0}", user.Name);
  12.  
  13.         SocialRatingManager ratingManager = new SocialRatingManager(serviceContext);
  14.         SocialRating[] ratings = ratingManager.GetRatings(profile);
  15.         foreach (SocialRating rating in ratings)
  16.         {
  17.             Console.WriteLine("Rated URL: ‘{0}’, rating title: ‘{1}’, rating: ‘{2}’, rating date: ‘{3}’", rating.Url, rating.Title, rating.Rating, rating.LastModifiedTime);
  18.         }
  19.  
  20.         Console.WriteLine("Comments of {0}", user.Name);
  21.  
  22.         SocialCommentManager commentManager = new SocialCommentManager(serviceContext);
  23.         SocialComment[] comments = commentManager.GetComments(profile);
  24.         foreach (SocialComment comment in comments)
  25.         {
  26.             Console.WriteLine("Commented URL: ‘{0}’, comment title: ‘{1}’, comment: ‘{2}’, comment date: ‘{3}’", comment.Url, comment.Title, comment.Comment, comment.LastModifiedTime);
  27.         }
  28.     }
  29.     else
  30.     {
  31.         Console.WriteLine("WARNING: User ‘{0}’ not found in user profile database", user.Name);
  32.     }
  33. }

The next code snippet provides an example for usage of the above methods.

  1. String url = "http://yoursite/";
  2. using (SPSite site = new SPSite(url))
  3. {
  4.     using (SPWeb web = site.OpenWeb())
  5.     {
  6.         // getting rating and comments for a specific URL
  7.         SPListItem listItem = web.Lists["YourList"].Items[0];
  8.         GetRatingsAndCommentsForListItem(listItem);
  9.  
  10.         // getting rating and comments for the current user
  11.         GetRatingsAndCommentsForUser(web.CurrentUser);
  12.         // getting rating and comments for a specific user
  13.         GetRatingsAndCommentsForUser(web.AllUsers[@"domain\user"]);
  14.     }
  15. }

The SocialTag class can be used similarly through the SocialTagManager object, but that manager class provides same extra methods might be worth a dedicated post later, especially since it is strongly related with another huge area of SharePoint 2010, namely the taxonomies.

In the following post I will show you how to set rating on items from code.

Advertisements

4 Comments »

  1. Hi Peter,

    You da man!!!!

    Thaks for this article, saved me alot of trouble with my development.

    Cheers and keep up the good work.
    Pieter

    Comment by Pieter — November 2, 2010 @ 09:18

  2. Hey Peter,

    Thanks a lot, It really helps a lot.

    Thanks a ton for your contribution..

    Regards,
    Sudhir

    Comment by Sudhir — December 4, 2010 @ 14:43

  3. […] an item from code By Peter Holpar In a post about a year ago I promised that I would show in my next post how to rate an item from […]

    Pingback by Rate an item from code « Second Life of a Hungarian SharePoint Geek — December 6, 2010 @ 00:16

  4. […] my last post I illustrated how to set rating in SharePoint 2010 from code. In a former post you can see how to get ratings of other users from code. I’ve assumed that if I have enough […]

    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


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: