3

Iam trying to enable the Rating settings programmatically but it seems not working . Iam not getting any error but it dont update the list with new columns LikeCount and LikedBy. I dont want to use PnP samples . Here is my code:

 private void SetProperty(ClientContext ctx, string listname, VotingExperience experience)
    {
       Web rootWeb = ctx.Site.RootWeb;
       List list = rootWeb.Lists.GetByTitle(listname);

        try
        {
            list.Context.Load(list.RootFolder, p => p.Properties);
            list.Context.ExecuteQueryRetry();

       list.RootFolder.Properties["Ratings_VotingExperience"] = experience.ToString();
            list.RootFolder.Update();
            list.Context.ExecuteQueryRetry();

       }
        catch (Exception ex)
        {
            //
        }
    }

Accessing :

SetRating(ctx, Lists.title, VotingExperience.Likes);

Is it correct way of doing ?? Anyone has experienced?

2 Answers 2

2

It seems you are trying with a invalid property name. Instead of Ratings_VotingExperience use Ratings_x005f_VotingExperience.

list.RootFolder.Properties["Ratings_x005f_VotingExperience"] = "Likes";

So question is: Where did I get that? In the following end-point:

/_api/Web/GetFolderByServerRelativeUrl('/Lists/{ListName}')/Properties

I have sent a GET request to the above URL and it returned me something like Ratings_x005f_VotingExperience.

Update

It's true above thing does not work! So I had a little R&D and made it working. Basically we need to add two site columns in list to make Rating thing working. I got the idea from Rating Settings itself.

enter image description here

Site columns are:

  • LikesCount which GUID ID is 6e4d832b-f610-41a8-b3e0-239608efda41
  • LikedBy which GUID ID is 2cdcd5eb-846d-4f4d-9aaf-73e8e73c7312

PS: These Guids are fixed

So I added these columns using below code and enabled Rating Settings

using(ClientContext clientContext = new ClientContext(targetSite)) {
    clientContext.Credentials = onlineCredentials;
    Web web = clientContext.Web;
    List list = web.Lists.GetByTitle("List Name");
    clientContext.Load(list);
    clientContext.ExecuteQuery();

    var likeCount = web.AvailableFields.GetById(new Guid("6e4d832b-f610-41a8-b3e0-239608efda41"));
    var likedBy = web.AvailableFields.GetById(new Guid("2cdcd5eb-846d-4f4d-9aaf-73e8e73c7312"));

    clientContext.Load(likeCount, p => p.SchemaXmlWithResourceTokens, p => p.Id, p => p.InternalName, p => p.StaticName);
    clientContext.Load(likedBy, p => p.SchemaXmlWithResourceTokens, p => p.Id, p => p.InternalName, p => p.StaticName);
    clientContext.ExecuteQuery();

    list.Fields.AddFieldAsXml(likeCount.SchemaXmlWithResourceTokens, true, AddFieldOptions.AddFieldInternalNameHint | AddFieldOptions.AddToAllContentTypes);
    list.Fields.AddFieldAsXml(likedBy.SchemaXmlWithResourceTokens, false, AddFieldOptions.AddFieldInternalNameHint | AddFieldOptions.AddToAllContentTypes);

    list.Update();
    clientContext.ExecuteQuery();
}

Above code will enable like/unlike. For other things, we have enable those also. Find here

private readonly Guid RatingsFieldGuid_AverageRating = new Guid("5a14d1ab-1513-48c7-97b3-657a5ba6c742");
private readonly Guid RatingsFieldGuid_RatingCount = new Guid("b1996002-9167-45e5-a4df-b2c41c6723c7");
private readonly Guid RatingsFieldGuid_RatedBy = new Guid("4D64B067-08C3-43DC-A87B-8B8E01673313");
private readonly Guid RatingsFieldGuid_Ratings = new Guid("434F51FB-FFD2-4A0E-A03B-CA3131AC67BA");
7
  • before enabling it has one more property Allow items in this list to be rated ? . can it be a problem ?? i dont know how to set true on that.
    – meer
    Feb 4, 2016 at 14:03
  • I guess, you should set "Likes" instead of Boolean Feb 4, 2016 at 14:06
  • i have following public enum VotingExperience { Ratings = 0, Likes = 1, } ok let me try
    – meer
    Feb 4, 2016 at 14:11
  • So is it working now?? Feb 4, 2016 at 14:12
  • no still not working :( , trying since morning but still with no luck
    – meer
    Feb 4, 2016 at 14:16
0
Clear-Host

Add-PSSnapin Microsoft.SharePoint.Powershell | Out-Null

$ass = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Portal")  

$web = Get-SPWeb http://url
$lst = $web.Lists["list"]

$reputationHelper = $ass.GetTypes().Where({$_.Name -eq "ReputationHelper"})[0]
$enableReputation = $reputationHelper.GetMethod("EnableReputation", ([System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic))
$enableReputation.Invoke($null, @($lst, "Likes", $false))
1
  • This will not work in SharePoint Online Feb 20, 2017 at 6:59

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.