0

I have a discussion board with posts on it.

Discussion Board 1

post1

post2

post3

I want to "like" the item in post2 in "Discussion Board 1". How would I do this via REST or JSOM. I have looked into the Social Feed and I can't understand how that works in relation to a specific list. It seems to pull back data based on a variety of information in the users profile.

1

you can use JSOM to like an Item

 var  setLike = function (context, listId, itemId, isLike, likescount) {
            SP.SOD.executeFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
                Microsoft.Office.Server.ReputationModel.Reputation.setLike(context, listId, itemId, isLike);
                context.executeQueryAsync(function () {

        }, function (sender, args) { console.error(args.get_message()) });
            });
            return false;
        };

isLike is a boolean value, you can get to know whether to like or unlike by

var getisLiked = function (likedby) {
        var isliked = false;
        if (likedby !== null && likedby !== undefined) {

            for (var i = 0; i < likedby.length; i++) {
                var like = likedby[i];
                if (like.get_lookupId() === _spPageContextInfo.userId) {
                    isliked = true;
                    break;
                }
            }

        }
        return isliked;
    };

likedBy is the Value of "LikedBy" column (internal name)

  • Where does reputation.js come from? I don't see that file anywhere on the SharePoint instance I am running on. – ArtfulDodger Aug 19 '15 at 15:13
  • Also, where does the parameter "likedby" come form in the second method? – ArtfulDodger Aug 19 '15 at 15:37
  • it comes from LikedBy(internalname) column of the list – sekhar Aug 19 '15 at 16:36
  • use get_fieldValues() method on listitem to get the JSON response, you can see LikedBy there – sekhar Aug 19 '15 at 16:37
  • reputation.js is "/_layouts/15/reputation.js", if it not registerd by default on you masterpage, call RegisterSod('reputation.js','/_layouts/15/reputation.js'); at the top of your js – sekhar Aug 19 '15 at 16:40
0
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var followingEndpoint;
var URL;
var website;
var clientContext;

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadWebsite);

function loadWebsite() {
    clientContext = SP.ClientContext.get_current();
    website = clientContext.get_web();
    clientContext.load(website);
    clientContext.executeQueryAsync(
        onRequestSucceeded,
        onRequestFailed);
}

function onRequestSucceeded() {
    URL = website.get_url();
    followingEndpoint = decodeURIComponent(URL) + "/_api/social.following";
    getFollowedDocs();
}

function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

function getFollowedDocs() {
    $.ajax({
        url: followingEndpoint + "/my/Followed(types=2)",
        headers: {
            "accept": "application/json;odata=verbose"
        },
        success: retrieveFollowedDocs,
        error: function (xhr, ajaxOptions, thrownError) {
            alert("GET error:\n" + xhr.status + "\n" + thrownError);
        }
    });
}

function retrieveFollowedDocs(data) {
    var stringData = JSON.stringify(data);
    var jsonObject = JSON.parse(stringData);
    var followed = jsonObject.d.Followed.results;
    //var threads = followed.results;
    var followedContent = '';
    followedContent +='<table><thead><tr><th>Title</th></tr></thead></table><ul>';

  for (var i = 0; i < followed.length; i++)  {
        //The Below if condition is for the Document limit. I need 5 documents to be displayed so given i=5. Based on your requirement you can the number.
    if (i ==5)  {
        var qryString = window.location.href;
        var qrySplit = qryString.split('?');
        if (qrySplit.length > 1)  {
            continue;
        }
        else  {
            break;
        }
        }

        var folDocs = followed[i];
        var NamewithExtn = folDocs.Name;
        var docName = NamewithExtn.split('.');
        var Name = docName[0];
        var ContentURL = folDocs.Uri;
        followedContent += '<li>';
        followedContent += '<table><tbody><tr><td><a href="'+URL+'">'+Name+'</a>';
        followedContent += '</td></tr></tbody></table>';
        followedContent += '</li>'
    }
    followedContent +='</ul>';
    $("#message").empty();
    $("#message").append(followedContent);
    innerTable = '';
}
</script>
</head>
<body>
    <div id="message"></div>
</body>
</html>  

Put a link for discussion, so you can follow a document by configure MySites. In MySites there is Document Library created with all the followed documents.

Use above code if you want to display the liked messages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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