0

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.

1
  • A quick check shows me reputation.js is not the file you need .. see answer below Commented Mar 2, 2017 at 13:27

1 Answer 1

0

EnsureScriptFunc() shouldn't be necessary if this is a user interaction...thus all async scripts will most likely have loaded by the time a user clicks .. no need to check if reputation.js has loaded

If you need the check either replace it with a plain vanilla JavaScript check to check for the Globals reputation.js adds

Or make sure to load scripts the SP.SOD (Script On Demand) SharePoint way:

https://www.eliostruyf.com/correctly-including-scripts-display-templates/

https://msdn.microsoft.com/en-us/library/office/ff410742(v=office.14).aspx

Update: Microsoft.Office.Server.Reputation

If you display a View with List set to do star-ratings,
then do a CTRL-SHFT-F search in the Console

This namespace comes from a different JS file:
(maybe it was reputation.js in SP2013 as all the blogs state)

1
  • Hi, thanks for the reply. I have removed the EnsureScriptFunc() from the function and made some updates. I am now receiving a new error, see update message above. Commented Mar 2, 2017 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.