2

Is it possible to create/enable rating column on a list using CSOM, as I tried but was unable to find any working code?

I was able to set the value to AverageRating column using JSOM. Is it possible using AddFieldAsXml?

Exact issue:

When creating a list there is a need to create columns dynamically using JSOM but I'm facing an issue while creating Star rating column.

I know it can be enabled in List Settings but is it possible to handle through code?

1

2 Answers 2

1

You can use the PnP Sample to enable rating in your list.

var ratingEnabler = new RatingsEnabler(clientContext);

ratingEnabler.Enable("ListName", VotingExperience.Ratings);

The value can be set as VotingExperience.Ratings or VotingExperience.Likes

Reference - Core.ListRatingSettings

Or you can use the below code. Here , the GUID is hardcoded and they remain the same across the tenant:

public static void EnableRating()
{
 string siteUrl = "https://sitecollectionurl.sharepoint.com/sites/test";
 string userName = "[email protected]";
 string password = "password";
 string listTitle = "List";


 Guid RatingsFieldGuid_AverageRating = new Guid("5a14d1ab-1513-48c7-97b3-657a5ba6c742");
 Guid RatingsFieldGuid_RatingCount = new Guid("b1996002-9167-45e5-a4df-b2c41c6723c7");
 Guid RatingsFieldGuid_RatedBy = new Guid("4D64B067-08C3-43DC-A87B-8B8E01673313");
 Guid RatingsFieldGuid_Ratings = new Guid("434F51FB-FFD2-4A0E-A03B-CA3131AC67BA");
 Guid LikeFieldGuid_LikedBy = new Guid("2cdcd5eb-846d-4f4d-9aaf-73e8e73c7312");
 Guid LikeFieldGuid_LikeCount = new Guid("6e4d832b-f610-41a8-b3e0-239608efda41");

 using (var ctx = = new ClientContext(siteUrl))
 {
    SecureString securePassword = new SecureString();
    foreach (char c in password.ToCharArray())
    {
        securePassword.AppendChar(c);
    }

    context.AuthenticationMode = ClientAuthenticationMode.Default;
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);

     ctx.Load(ctx.Web);
     ctx.ExecuteQuery();

     List filesLibrary = ctx.Web.GetListByTitle(listTitle);

     ctx.Load(filesLibrary);

     ctx.Load(filesLibrary);
     ctx.ExecuteQuery();

     ctx.Load(filesLibrary.RootFolder, p => p.Properties);
     ctx.ExecuteQuery();

     filesLibrary.RootFolder.Properties["Ratings_VotingExperience"] = "Likes";
     filesLibrary.RootFolder.Update();

     ctx.ExecuteQuery();

     EnsureField(filesLibrary, RatingsFieldGuid_RatingCount, ctx);
     EnsureField(filesLibrary, RatingsFieldGuid_RatedBy, ctx);
     EnsureField(filesLibrary, RatingsFieldGuid_Ratings, ctx);
     EnsureField(filesLibrary, RatingsFieldGuid_AverageRating, ctx);
     EnsureField(filesLibrary, LikeFieldGuid_LikedBy, ctx);
     EnsureField(filesLibrary, LikeFieldGuid_LikeCount, ctx);

     filesLibrary.Update();
     ctx.ExecuteQuery();

     ctx.Load(filesLibrary, view => view.DefaultView);
     ctx.ExecuteQuery();

     var defaultView = filesLibrary.DefaultView;
     defaultView.ViewFields.Add("LikesCount");

     defaultView.Update();
     ctx.ExecuteQuery();
 }
}

private static Field EnsureField(List list, Guid fieldId, ClientContext context)
{
 FieldCollection fields = list.Fields;

 FieldCollection availableFields = list.ParentWeb.AvailableFields;
 Field field = availableFields.GetById(fieldId);

 context.Load(fields);
 context.Load(field, p => p.SchemaXmlWithResourceTokens, p => p.Id, p => p.InternalName, p => p.StaticName);
 context.ExecuteQuery();

 if (!fields.Any(p => p.Id == fieldId))
 {
     var newField = fields.AddFieldAsXml(field.SchemaXmlWithResourceTokens, false, AddFieldOptions.AddFieldInternalNameHint | AddFieldOptions.AddToAllContentTypes);
     return newField;
 }
 return field;
}

Enable Ratings/Likes For A List In SharePoint Office 365 Programmatically Using C#

Note - in case there is some issue with rating not getting enabled, you can replace the property bag key as Ratings_x005f_VotingExperience and then check. See the link in the comments.

1
  • hi @vinodkumar , did you try the above code, is it working for you ? Commented Jun 14, 2017 at 11:52
0

AFAIK, Microsoft hasn't given any direct approach to enable ratings in a list using CSOM, as mentioned in the in the comments by Gautam Sheth, there might be some work around but I haven't tried it yet, so I'm not sure about it.

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.