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


Sathish Nadarajan
SharePoint MVP
Published On :   28 Aug 2016
Visit Count
Today :  1    Total :   3826
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan