none
Implementing custom "likes" in SharePoint 2013

    Question

  • We're developing a new intranet using SharePoint 2013 and I have a question about the "likes" functionality. I've created a custom list and I've turned on the rating (Rating settings) for the items in this list using the "Likes" alternative.

    So far, so good. I can like and unlike any item in the detailed list view (AllItems.aspx).

    But my users are not going to see that list. Items will only be available to the users in a usercontrol that I've added to my pagelayout. The usercontrol will show number of likes and contain a Like button/link to let the current user like the item too.

    The number of likes for a particular item is stored in the likesCount field, which is added to the list when the rating is turned on. I can read the likesCount field like any other field, but how can I update this information? I obviously need to store information about the logged in user as well.

    Since the likes button is standard i SharePoint 2013, there must be a way to hook up to this particular functionality programmatically.

    Please help or point me in the right direction. Any code examples in c# would be appreciated.


    Wednesday, December 19, 2012 7:59 PM

Answers

  • Hi Papageno,

    When a user likes an item,SharePoint also logs which user has liked the item and that's the reason it shows unlike. so you don't have to hookup any user related info for the like.

    here is the sample code to like an SharePoint list item

    SPList resourceList = SPContext.Current.Web.Lists["Resources"];
    SPListItem likeItem = resourceList.Items[0];    
    Reputation.SetLike(resourceList.ID.ToString(), likeItem.ID, true);
    Hope this helps, this post might help you know more about Reputation Class


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, January 18, 2013 2:57 PM

All replies

  • I'm still struggling with this one. Please help if you have any idea.
    Tuesday, December 25, 2012 10:29 PM
  • Hi,

    There is a API SetLike to like/unlike an item and the same API returns Total number of likes once use likes the item.

    http://msdn.microsoft.com/en-us/library/jj195136(office.12).aspx

    I think Person who liked the item would be automatically picked as Current Logged in User and used in SetLike Method.

    I hope this information helps.


    Mark as Answer if it solves your problem. Vote As Helpful if the post is helpful for you.

    Wednesday, January 09, 2013 4:40 PM
  • Thanks for your reply, Gajendra. Unfortunately, I have no experience in CSOM development. If someone could please take the time to give me a C# example on how to "like" an SPListItem, I would be very happy.
    Friday, January 18, 2013 10:09 AM
  • Hi Papageno,

    When a user likes an item,SharePoint also logs which user has liked the item and that's the reason it shows unlike. so you don't have to hookup any user related info for the like.

    here is the sample code to like an SharePoint list item

    SPList resourceList = SPContext.Current.Web.Lists["Resources"];
    SPListItem likeItem = resourceList.Items[0];    
    Reputation.SetLike(resourceList.ID.ToString(), likeItem.ID, true);
    Hope this helps, this post might help you know more about Reputation Class


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Friday, January 18, 2013 2:57 PM
  • Thank, Raghavendra!

    Exactly on the spot what I was asking for. You really saved my day! Great blog you've got, by the way.

    Tuesday, January 22, 2013 10:02 AM
  • Thank you :)

    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, January 22, 2013 11:38 AM
  • Thanks for this answer ! 

    Now it is time for the next step : how to determine if the current user has already liked this page ?


    Blog Sharepoint : www.paslatek.net Twitter : @LimozinLionel


    Tuesday, September 17, 2013 9:19 AM
  • I'm not sure if that's possible, but you could get a list of all people liking this object and then check if the list contains the current user.

    Get a list like this:

            public List<string> GetLikedBy(SPListItem listItem)
            {
                List<string> returnUsers = new List<string>();
    
                try
                {
                    SPFieldUserValueCollection likers = (SPFieldUserValueCollection)listItem["LikedBy"];
                    foreach (SPFieldUserValue value in likers)
                    {
                        returnUsers.Add(value.User.LoginName);
                    }
                }
                catch { }
    
                return returnUsers;
            }
    

    And then check if the current user is in the list:

    loginName = users.Find(d => d.LoginName == user.LoginName).LoginName;

    if (loginName == string.Empty)
    {
    // Current user doesn't like this item.
    }

    I haven't tested this code, but I think you'll get the idea.

    Tuesday, September 17, 2013 10:03 AM
  • Here is the answer :

    There is a field "LikedBy" with a value of type SPFieldUserValueCollection containing all users who had like the item.

    Here is a sample code to determine if current user like the item :

    //collListItem[0] contain the SPListItem
    SPFieldUserValueCollection likedBy = (SPFieldUserValueCollection)collListItem[0]["LikedBy"];
    int cnt = likedBy.Where(u => SPContext.Current.Web.CurrentUser.LoginName.ToLower().Contains(u.User.LoginName.ToLower())).Count();
    return cnt > 0;


    Blog Sharepoint : www.paslatek.net Twitter : @LimozinLionel

    • Proposed as answer by lionel limozin Tuesday, September 17, 2013 10:05 AM
    Tuesday, September 17, 2013 10:04 AM
  • And here is how to count like count, fairly simple :

    return (int)(double)collListItem[0]["LikesCount"];


    Blog Sharepoint : www.paslatek.net Twitter : @LimozinLionel

    • Proposed as answer by lionel limozin Tuesday, September 17, 2013 10:05 AM
    Tuesday, September 17, 2013 10:05 AM
  • You ask a question to which you already know the answer.

    Then you propose your own reply as answer...

    Nice one.


    Thursday, September 19, 2013 1:15 PM
  • Sure it is ! 

    If you look at the date time where if post the question and the answer you will see a delay of 30 min, time took to find the answer in another place.... 

    So I thought it is a good idea to share here the answer with the community... 

    Maybe you think I did that for "points" in the forum but I don't need that, just want help :)


    Blog Sharepoint : www.paslatek.net Twitter : @LimozinLionel

    Thursday, September 19, 2013 1:27 PM
  • I have tried above code and getting below error:

    "Updates are currently disallowed on GET requests.  To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb."

    My code contains Web.AllowUnsafeUpdates = true; also checked with RunWithElevatedPrivileges.

    Please suggest!

    Monday, February 08, 2016 5:55 AM
  • Hi Papageno,

    When a user likes an item,SharePoint also logs which user has liked the item and that's the reason it shows unlike. so you don't have to hookup any user related info for the like.

    here is the sample code to like an SharePoint list item

    SPList resourceList = SPContext.Current.Web.Lists["Resources"];
    SPListItem likeItem = resourceList.Items[0];    
    Reputation.SetLike(resourceList.ID.ToString(), likeItem.ID, true);
    Hope this helps, this post might help you know more about Reputation Class


    Raghavendra Shanbhag | Blog: www.SharePointColumn.com
    Please click "Propose As Answer " if a post solves your problem or "Vote As Helpful" if a post has been useful to you.
    Disclaimer: This posting is provided "AS IS" with no warranties.

    I have a community list, let's call it Hot Trending for example. I created a post where user can like/reply. I am trying to emulate the like function from my web part. So I did the following as a test:

         Reputation.SetLike("{Hot Trending List ID}", {The ID of the first item}, true);

    When the above line executes, I get the following error: 

         We can't assign that badge to any more members. Gifted badges are meant to acknowledge a small set of people in the community and you already have that one assigned to 100 members.

    Why am I receiving the error and How can I resolve it?

    Thursday, May 12, 2016 3:10 PM
  • this is using server side object model the problem is that i want SPFieldUserValueCollection in Csom and how to access and manipulate it in fact in perticular to how to manipulate LikeBy, NoOfLikes, RatedBy, AverageRating, UserRating all strictly using CSOM.

    Friday, September 16, 2016 1:23 PM
  • Did you ever get a solution to the "We can't assign that badge to any more members. Gifted badges..." message? I have the exact same problem and I would love to know how to handle the problem.
    Tuesday, February 28, 2017 5:23 PM