none
Implementing custom "likes" in SharePoint 2013

    Domanda

  • 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.


    mercoledì 19 dicembre 2012 19:59

Risposte

  • 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.

    • Contrassegnato come risposta Papageno Sweden martedì 22 gennaio 2013 09:58
    venerdì 18 gennaio 2013 14:57

Tutte le risposte

  • I'm still struggling with this one. Please help if you have any idea.
    martedì 25 dicembre 2012 22:29
  • 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.

    mercoledì 9 gennaio 2013 16:40
  • 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.
    venerdì 18 gennaio 2013 10:09
  • 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.

    • Contrassegnato come risposta Papageno Sweden martedì 22 gennaio 2013 09:58
    venerdì 18 gennaio 2013 14:57
  • Thank, Raghavendra!

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

    martedì 22 gennaio 2013 10:02
  • 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.

    martedì 22 gennaio 2013 11:38
  • 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


    martedì 17 settembre 2013 09:19
  • 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.

    martedì 17 settembre 2013 10:03
  • 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

    • Proposto come risposta lionel limozin martedì 17 settembre 2013 10:05
    martedì 17 settembre 2013 10:04
  • And here is how to count like count, fairly simple :

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


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

    • Proposto come risposta lionel limozin martedì 17 settembre 2013 10:05
    martedì 17 settembre 2013 10:05
  • You ask a question to which you already know the answer.

    Then you propose your own reply as answer...

    Nice one.


    giovedì 19 settembre 2013 13:15
  • 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

    giovedì 19 settembre 2013 13:27
  • 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!

    lunedì 8 febbraio 2016 05:55
  • 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?

    giovedì 12 maggio 2016 15:10
  • 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.

    venerdì 16 settembre 2016 13:23
  • 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.
    martedì 28 febbraio 2017 17:23