0

I enabled the Rating Scale on a list and it works well on list view. But, is there a way to add it in custom DispForm.aspx while enabling user to rate it dynamically when DispForm is opened? It shows on default DispForm.aspx but not when I create a custom one.

I also read this post but need to research more about PnP and how I could use the solution posted.

enter image description here

Edit 4/11/19

I found this post but could not figure out how to modify the following within my scenario. My list name is Training where I enabled the rating system. Below is what I have so far, the GetRating() works but I still can't get the SetRating() to work.

The code is in txt file and linked in a CEWP located below the main DispForm.aspx contents.

I started with

$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(GetRating, "sp.js");
});

Modified GetRating function:

function GetRating() {

var listUri = _spPageContextInfo.webServerRelativeUrl;
var itemID = GetUrlKeyValue("ID", false, location.href);
var spCtx = new SP.ClientContext(listUri);
var list = spCtx.get_web().get_lists().getByTitle("Training");

if(itemID > 0 ) {
    this.itemRating= list.getItemById(itemID);
    spCtx.load(itemRating);
    spCtx.executeQueryAsync (
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    );
}  
}

Modified SetRating function that does not work. It doesn't give any error messages.

function SetRating(rValue) {
var listUri = _spPageContextInfo.webServerRelativeUrl;
var itemID = GetUrlKeyValue("ID", false, location.href);
var listGUIDURL = listUri +"/_api/web/lists/getByTitle('Training')?&select=Id";
//var listGUID = "";
var listGUID = _spPageContextInfo.pageListId;

$.ajax({        
    url: listGUIDURL,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        if (data.d.Id != "") {   
            //listGUID = data.d.Id; 
            var spCtx = new SP.ClientContext(listUri);
            EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
                Microsoft.Office.Server.ReputationModel.Reputation.setRating(spCtx, listGUID, itemID, rValue);
                spCtx.executeQueryAsync(
                    function() {
                        SP.UI.Notify.addNotification("Thank you for rating.", false);
                        GetRating();
                    }, function() {
                        SP.UI.Notify.addNotification("There was an error saving.", false);                         
                });
            });
        }
    }, 
    error: function (data) { console.log("Failed to set rating"); }
});
}

When using the original code (without EnsureScriptFunc line, it gives an error message Uncaught TypeError: Cannot read property 'Server' of undefined. Using this reference, I added EnsureScriptFunc... which no longer gives an error message but it doesn't do anything.

I also added the following in my master HTML:

<!--SPM:<SharePoint:ScriptLink ID="ScriptLink6" name="SP.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />-->
<!--SPM:<SharePoint:ScriptLink ID="ScriptLink8" name="SP.Core.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />-->
<!--SPM:<SharePoint:ScriptLink ID="ScriptLink9" name="Reputation.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />-->

1 Answer 1

0

I finally got this to work. Rather than adding the links in my master HTML, I added the following SP.SOD... in:

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(GetRating, "sp.js");
SP.SOD.registerSod('reputation.js', '/_layouts/15/reputation.js');
SP.SOD.executeFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetRating);
});
});

Source:

Your Answer

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

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