Skip to content

Instantly share code, notes, and snippets.

Last active March 17, 2021 10:44
Show Gist options
  • Save sjkp/5563092 to your computer and use it in GitHub Desktop.
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=, 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() {
_spPageContextInfo.pageListId.substring(1, 37),
_spPageContextInfo.pageItemId, !likedPage);
function() {
var elements = document.getElementsByClassName('likecount');
if (likedPage) {
} else {
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;
<a href="#" onclick="(function() {likepage.LikePage();})()"><span class="likecount"></span></a>
Copy link

bonm014 commented May 11, 2016

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.

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!

Copy link

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

Copy link

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!


Copy link

in line 45
change from
if ($v_3.$1E_1 === _spPageContextInfo.userId) { cb(true, item.get_item('LikesCount')); }
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