I've been attempting to update the rating values (shown as star ratings) for items that I am fetching from a list via REST and displaying in a Content Editor Webpart.
I am essentially rebuilding the star rating functionality.
I am successfully pulling and displaying the average star rating, however I now need to update star rating from my custom page via JavaScript/JSOM
The function that I am planning to attach to each star is as follows:
function SetRating(ratingObj) {
//http://sharepointpals.com/post/SetRating-in-Reputation-Class-in-SharePoint-2013-using-Javascript-and-C-CSOM
$.ajax({
url:ratingObj.globalUrl +"/_api/web/lists/getbytitle('" + ratingObj.listName + "')?select=Id",
async: false,
type: "GET",
headers: {
"content-type": "application/json;odata=verbose","accept": "application/json;odata=verbose"
},
success: function (data) {
var clientContext;
var website;
console.log(data);
var listID = data.d.Id;
//var spCtx = clientContext;
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
var spCtx = new SP.ClientContext(website);
console.log(spCtx);
Microsoft.Office.Server.ReputationModel.Reputation.setRating(spCtx, listID, ratingObj.itemID, ratingObj.ratingValue);
spCtx.executeQueryAsync(Function.createDelegate(this, this.RatingSuccess), Function.createDelegate(this, this.RatingFailure));
function RatingSuccess(sender, args) {
alert('Rating Done Successfully');
}
function RatingFailure(sender, args) {
alert('SetRating failed:' + args.get_message());//note that you will get error if try like twice
}
},
error: function (data) {
console.log(data);
}
});
}
I am passing all parameters in as an object. My problem is that the line: Microsoft.Office.Server.ReputationModel.Reputation.setRating(spCtx, listID, ratingObj.itemID, ratingObj.ratingValue);
Does not seem to run, however I am not too sure why. I am pretty new to CSOM/JSOM so I am not too sure what else to try.
Further info
The list I am querying is on the parent site to the site this code is running on.
I am not receiving the alerts in the code at all so the function seems to be completely failing on the above line. I am also not getting any errors logged in the developer tools console.
I am referencing the required files to my knowledge:
<SharePoint:ScriptLink name='sp.js' runat='server' ondemand='false' localizable='false' loadafterui='true' />
<SharePoint:ScriptLink name='sp.runtime.js' runat='server' ondemand='false' localizable='false' loadafterui='true' />
<SharePoint:ScriptLink name='sp.core.js' runat='server' ondemand='false' localizable='false' loadafterui='true' />
<SharePoint:ScriptLink name='reputation.js' runat='server' ondemand='false' localizable='false' loadafterui='true' />
Any help would be much appreciated. Let me know if any further info/clarification is required
Thanks!
UPDATE:
I have updated the SetRating function above, and everything is working except that I now receive Microsoft.Office is undefined. This happens after page load as I am executing the above function on a click event that I am attaching to the star rating "a" tags on the page.
reputation.js
is not the file you need .. see answer below