SharePoint Pals
 | Sign In
How to Enable Ratings/Likes For a List in SharePoint Office 365 Programmatically using PNP C#
by Sathish Nadarajan 25 Aug 2016
Author
SharePoint MVP
Visits   
Today  :   6     Total  :    2565

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation