Skip to content

Instantly share code, notes, and snippets.

@sjkp
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=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>
@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