How to Get/Set Ratings in SharePoint Office 365 Item Programmatically using Client Side Object Model (CSOM)

Sathish Nadarajan
 
Solution Architect
August 28, 2016
 
Rate this article
 
Views
6900

In the earlier article, we saw how to enable Rating in Office 365 environment. In this article, as a continuation, we need to set the rating or get the rating value of an Item using CSOM. i.e., I am going to Like or UnLike an Item in the document library using CSOM. Again, these piece of codes are very straight forward and as part of a bigger requirement, these are small function points, which I used to create it as a separate console and integrate later to the actual project code.

Basically, the operations I am performing is as follows.

1. Get the List Item

2. Get the Current Liked Users

3. Get the Current Liked Count

4. Increment the Count

5. Add the new users to the existing list.

6. Update the list Item with new values.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 using Microsoft.SharePoint.Client;
 using System.Security;
 using Microsoft.Online.SharePoint.TenantAdministration;
 
 namespace Console.Office365
 {
     class Program
     {
         static void Main(string[] args)
         {
             LikeUnLikeCSOM();
         }
 
 
         public static void LikeUnLikeCSOM()
         {
             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";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 ctx.Load(ctx.Web);
                 ctx.ExecuteQuery();
 
                 List filesLibrary = null;
 
                 filesLibrary = ctx.Web.GetListByTitle(listTitle);
 
                 ctx.Load(filesLibrary);
                 ctx.ExecuteQuery();
 
                 ListItem item = filesLibrary.GetItemById(1);
 
                 ctx.Load(item);
                 ctx.ExecuteQuery();
 
                 List<FieldUserValue> newLikedBy = new List<FieldUserValue>();
 
                 var likedBy = item["LikedBy"] as FieldUserValue[];
 
                 if (likedBy != null)
                 {
                     foreach (var liked in likedBy)
                     {
                         newLikedBy.Add(liked);
                     }
                 }
                 newLikedBy.Add(FieldUserValue.FromUser("sathish@sppalsmvp.onmicrosoft.com"));
 
                 item["LikedBy"] = newLikedBy;
                 item["LikesCount"] = Convert.ToInt32(item["LikesCount"]) + 1;
                 item.Update();
 
                 ctx.Load(item);
                 ctx.ExecuteQuery();
  
             }
         }
     }
     
 }
 

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