var Reputation = Reputation || {}; | |
Reputation = (function () { | |
function executeJson(options) { | |
var headers = options.headers || {}; | |
headers["Accept"] = "application/json;odata=verbose"; | |
if (options.method == "POST") { | |
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val(); | |
} | |
var ajaxOptions = | |
{ | |
url: options.url, | |
type: options.method, | |
contentType: "application/json;odata=verbose", | |
headers: headers | |
}; | |
if (options.method == "POST") { | |
ajaxOptions.data = JSON.stringify(options.payload); | |
} | |
return $.ajax(ajaxOptions); | |
} | |
function getListItem(webUrl, listTitle, itemId) { | |
var options = { | |
url: webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")", | |
method: "GET" | |
}; | |
return executeJson(options); | |
} | |
function updateListItem(webUrl, listTitle, itemId, itemPayload) { | |
var options = { | |
url: webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")", | |
method: "POST", | |
headers: { | |
"X-HTTP-Method": "MERGE", | |
"If-Match": "*" | |
}, | |
payload: itemPayload | |
}; | |
return executeJson(options); | |
} | |
return { | |
setLike: function (webUrl, listTitle, itemId, setLike) { | |
//1.retieve existing ratings | |
return getListItem(webUrl, listTitle, itemId) | |
.then(function (data) { | |
//2.set like for current user | |
var userKey = _spPageContextInfo.userId.toString(); | |
var likes = data.d.LikesCount; | |
var updated = false; | |
var userKeys = data.d.LikedByStringId == null ? [] : data.d.LikedByStringId.results; | |
var posKey = userKeys.indexOf(userKey); | |
if (posKey == -1) { | |
if (setLike) { | |
userKeys.push(userKey); | |
likes++; | |
updated = true; | |
} | |
} | |
else { | |
if (!setLike) { | |
userKeys.splice(posKey, 1); | |
likes--; | |
updated = true; | |
} | |
} | |
if (updated) { | |
var itemProperties = { | |
"__metadata": data.d.__metadata, | |
"LikedByStringId": {"results": userKeys}, | |
LikesCount: likes | |
}; | |
return updateListItem(webUrl, listTitle, itemId, itemProperties); | |
} | |
return data; | |
}); | |
} | |
}; | |
})(); |
This comment has been minimized.
This comment has been minimized.
Hey, thanks for this snippet! This cannot work for default SharePoint 2013 environments. :-( |
This comment has been minimized.
This comment has been minimized.
I had the same problem. It may be that SharePoint is just setup differently. I made a couple of minor changes and got it working:
|
This comment has been minimized.
This comment has been minimized.
@3bduuu: Thank you! With |
This comment has been minimized.
This comment has been minimized.
Can someone please help - how do I include this script as part of an SPFX webpart? |
This comment has been minimized.
This comment has been minimized.
@jasong1987 Have a look at @pnp/sp/comments - accessing likes got much easier -> https://github.com/pnp/pnpjs/blob/dev/packages/sp/docs/comments-likes.md |
This comment has been minimized.
This comment has been minimized.
I am getting error as below in SharePoint online: can some one please help me for this. |
This comment has been minimized.
This comment has been minimized.
Is there a way to fetch _spPageContextInfo.userId over an api fetch request? I am trying to implement some functionality in an app, but don't know how to get the _spPageContextInfo object over an api call. |
This comment has been minimized.
Usage