0

I'm trying to implement the standard like function to a custom view I wrote.

My code for getting the items, getting LikesCount and how I display them:

<script type="text/javascript">

$(document).ready(function() {    
    // Rest Call
    var requestUri = "/_api/lists/getbytitle('Shout')/items";

    var llength = "";

        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function (data) {
                $.each(data.d.results, function (i, item){
                    var title = item.Title;
                    var description = item.Description;
                    var pictureURLDesc = item.PictureURL.Description;
                    var pictureURL = item.PictureURL.Url;   
                    var linkDesc = item.Link.Description;
                    var linkURL = item.Link.Url;    
                    var likesCount = item.LikesCount;               

                    try 
                    {
                        llength = description.slice(0, 120);
                    } 
                    catch ( err ) 
                    { 
                        alert( err ); 
                    }               

                    // alert("The item has " + likesCount + " likes");

                    // Set News item                        
                    document.getElementById("head" + i).innerHTML = "<div class='thumbnail-img'><div class='overflow-hidden'><img class='img-responsive' src=" + pictureURL + " alt=" + pictureURLDesc + "/> &#160;</div><a class='btn-more hover-effect' href='#'><i class='icon-thumbs-up'></i> " + likesCount + "</a></div><div class='caption'><h3><a class='hover-effect' href='#'>" + title + "</a></h3><p>" + llength + "</p></div>";

                })
            },
            error: function () {
                alert("Error getting items");
            }                     
        });    

});

The function is turned on in the list and I can rate / unrate the items there.

enter image description here

This is how it is displayed by now:

enter image description here

Now I want this functionality in my script. I found several posts how to like a page, but nothing which helps me to like a list item.

I would like to call the function in here:

<a class='btn-more hover-effect' href='#'>

I can get the current LikesCount with REST, but how to set and unset?

Any Step-by-Step guides known?

1

1 Answer 1

2

Here is a similar Question and I answered there. Liking a discussion board list item

We have an OOTB JS library "reputation.js". Load the Library and call the function Microsoft.Office.Server.ReputationModel.Reputation.setLike to like/unlike a list item. Other source here. Do not forget to upvote Vadim Gremyachev's answer if helped.

6
  • Sorry, but I'm a huge noob if it comes to implementing this into my code. I guess it is the right way, but could you help me there a little bit? That would be awesome!
    – Patrick
    Sep 7, 2015 at 11:33
  • I think you can understand better from this article. sharepointstuffs.com/… .All we need is the list id and listitem id for setLike function. Ignore _spPageContextInfo.pageListId and _spPageContextInfo.pageItemId in this article and pass your list id and listitem id instead. Sep 7, 2015 at 11:56
  • It is finally working... Thanks for your help. I will try to upload a blog post, how everthing worked out for me.
    – Patrick
    Sep 9, 2015 at 11:08
  • Also postet now my scripts and all I needed here: blog.afrait.com/like-a-list-item-facebook-like
    – Patrick
    Sep 16, 2015 at 8:22
  • Great. I regularly follow this site, Interesting , it is yours. :) Sep 16, 2015 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.