Skip to content

Instantly share code, notes, and snippets.

@sjkp
Last active March 17, 2021 10:44
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sjkp/5563092 to your computer and use it in GitHub Desktop.
SharePoint 2013, how to enable like/unlike functionality by client side javascript on a publishing pagelayout. - Requirements, like functionality is enabled on the page library. - You must include reputation.js if you are wrapping this in a webcontrol, that can e.g. be done with: ScriptLink.RegisterScriptAfterUI(this.Page, "reputation.js", false);
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<SharePointWebControls:ScriptLink language="javascript" name="reputation.js" OnDemand="false" LoadAfterUI="true" runat="server" Localizable="false" />
var likepage = {
//Likes the current page.
LikePage: function () {
likepage.getUserLikedPage(function(likedPage, likeCount) {
var aContextObject = new SP.ClientContext();
EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function() {
Microsoft.Office.Server.ReputationModel.
Reputation.setLike(aContextObject,
_spPageContextInfo.pageListId.substring(1, 37),
_spPageContextInfo.pageItemId, !likedPage);
aContextObject.executeQueryAsync(
function() {
var elements = document.getElementsByClassName('likecount');
if (likedPage) {
likeCount--;
} else {
likeCount++;
}
for (var i = 0; i < elements.length;i++) {
elements[i].innerHTML = likeCount;
}
}, function(sender, args) {
// Custom error handling if needed
});
});
});
},
// Checks if the user already liked the page, and returns the number of likes.
getUserLikedPage: function (cb) {
var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
var item = list.getItemById(_spPageContextInfo.pageItemId);
context.load(item, "LikedBy", "ID", "LikesCount");
context.executeQueryAsync(Function.createDelegate(this, function (success) {
// Check if the user id of the current users is in the collection LikedBy.
var $v_0 = item.get_item('LikedBy');
if (!SP.ScriptHelpers.isNullOrUndefined($v_0)) {
for (var $v_1 = 0, $v_2 = $v_0.length; $v_1 < $v_2; $v_1++) {
var $v_3 = $v_0[$v_1];
if ($v_3.$1E_1 === _spPageContextInfo.userId) {
cb(true, item.get_item('LikesCount'));
}
}
}
cb(false, item.get_item('LikesCount'));
}),
Function.createDelegate(this, function (sender, args) {
//Custom error handling if needed
}));
},
initialize: function () {
var elements = document.getElementsByClassName('likecount');
likepage.getUserLikedPage(function(likedPage, likesCount) {
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = likesCount;
}
});
}
};
_spBodyOnLoadFunctionNames.push("likepage.initialize");
<a href="#" onclick="(function() {likepage.LikePage();})()"><span class="likecount"></span></a>
@bonm014
Copy link

bonm014 commented May 11, 2016

Thanks!
Please update the code with the following snippet


var likedByUsers = item.get_item('LikedBy');
if (!SP.ScriptHelpers.isNullOrUndefined(likedByUsers)) {
for (var $v_1 = 0, $v_2 = likedByUsers.length; $v_1 < $v_2; $v_1++) {
var likedByUser = likedByUsers[$v_1];
if (likedByUser.get_lookupId() === _spPageContextInfo.userId) {
cb(true, item.get_item('LikesCount'));
}
}

}

The $1E_1 variable in invalid at my environment (was $1W_1) and I had success with the get_lookupId() function.

@jyendapally
Copy link

Hi, We are trying to set the like on a list . However, when using your code EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function() {
8 Microsoft.Office.Server.ReputationModel.
9 Reputation.setLike(aContextObject,
10 _spPageContextInfo.pageListId.substring(1, 37),
11 _spPageContextInfo.pageItemId, !likedPage);
and passing list id and item id, is not working. It's not even executing the code where we say EnsureScriptFunc(). Any insights on this would really help. Thanks!

@jyendapally
Copy link

By the way, we are using javascrit and trying to set the LIKES on a list which is on the cloud

@simonagren
Copy link

jyendapally,
I think that the code isnt executed since the script cannot be "Ensured", you havnt included the "reputation.js" file.

Either in the page layout via scriptlink or just "<script src="_layouts/15/reputation.js"></script>".

This works for me in SharePoint Online!

/Simon

@OmarRAMIC
Copy link

in line 45
change from
if ($v_3.$1E_1 === _spPageContextInfo.userId) { cb(true, item.get_item('LikesCount')); }
to
if ($v_3.$1E_1 === _spPageContextInfo.userId) { cb(true, item.get_item('LikesCount')); return; }

without the return , cb function is called twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment