-->

How to Check Rating Settings is enabled or not by

2019-06-10 06:52发布

问题:

I have created custom Ribbon Button and I want to check Rating Settings is enable or not. If Rating Settings is Enable then Enable Ribbon Button, else Disable Ribbon Button.

This is code to get current List, but I don't see any function to check Rating Settings:

   var clientContext = new SP.ClientContext();
   var oWebsite = clientContext.get_web();
   var collList = oWebsite.get_lists();

   var listId = SP.ListOperation.Selection.getSelectedList();
   var sdlist = oWebsite.get_lists().getById(listId);

   clientContext.load(sdlist);

   function CheckRatingSettings(){
       var fields = sdlist.get_fields();
       //What next to check Ratting Setting, I can't find any function to get that
   }

回答1:

I dont think you can query that from the client API. Your best bet should be to implement a WCF service that checks that on the server and then query that Service through javascript.

Check CKS developer tools, they have among other goodies a template for visual studio to create WCF services on Sharepoint.

Another option is to check if the field "AverageRating" exists on the list, this field is added to the list when you enable ratings. Bear in mind that the field might exist but the ratings may be disabled though.

This is not tested (and assuming your code is getting the right listid, something along the lines of this:

var fields = slList.get_fields();
clientContext.load(fields );


function CheckRatingSettings(fields){
       for (var i = 0; i < fields.length; i++) {
           if (fields[i].internalName == "AverageRating") {
               return true;
           }
        }
        return false;
}