How to Enable Ratings/Likes For a List in SharePoint Office 365 Programmatically using PNP C#

Sathish Nadarajan
 
Solution Architect
August 25, 2016
 
Rate this article
 
Views
8620

Recently I met with a requirement to enable the Rating feature in SharePoint Office 365 List/Library Programmatically. It is very straight forward and simple to implement.

Thought of sharing the small snippet to the Community.

To enable the Rating, we need to Add 6 Site Columns to the List/Library. The 5 Site Columns are as follows.

 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");
 

4 for Ratings and 2 for Likes. As all of us know, the Rating Settings contains 2 types. Either it can be Likes or Star Ratings.

 

image

 

The Console Application will be as follows.

 class Program
     {
         static void Main(string[] args)
         {
             EnableRating();
         }
 
 
         public static void EnableRating()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://**********.sharepoint.com/sites/DeveloperSite";
             string userName = "Sathish@********.onmicrosoft.com";
             string password = "********";
             string listTitle = "MyDocLib";
 
 
             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 = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 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;
         }
     }
 

Just updating the Property Bag Value with “Likes” or “Ratings” will do the magic for us. Provided those 6 site columns should be added.

 filesLibrary.RootFolder.Properties["Ratings_VotingExperience"] = "Likes";
 filesLibrary.RootFolder.Properties["Ratings_VotingExperience"] = "Ratings";
 filesLibrary.RootFolder.Properties["Ratings_VotingExperience"] = "";
 

If we want to disable rating, then the Property Bag Value will be empty.

Hope the small snippet will help a lot of effort.

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment