Programmatically LIKE and UNLIKE a SharePoint List Item

Before do this, Ensure "Likes" is enabled in "Rating Settings" under "List Settings"
There are two ways to like and Unlike an Item

1. Reputation Class

Reputation.SetLike(ListID,ItemID,true);
If you want to unlike an item u need to write the below code
Reputation.SetLike(ListID,ItemID,false);

2. Directly updating the List fields

SPFieldUserValueCollection likedBy = new SPFieldUserValueCollection(web, item["LikedBy"].ToString()); 
SPUser user = web.EnsureUser("UserName");
SPFieldUserValue newUser = new SPFieldUserValue(web, user.ID, user.Name); 
likedBy.Add(newUser);
int likes = likedBy.Distinct().Count();
item["LikesCount"] = likes;
item["LikedBy"] = likedBy;

item.SystemUpdate(false);

Comments

  1. Reputation.SetLike(ListID,ItemID,false);

    How i will use this in sharepoint 2013.

    ReplyDelete

  2. thisItem.set_item('LikesCount',itemc);
    thisItem.set_item('LikedBy',44);
    thisItem.update();
    This code working perfectly for Like.
    Can you please help me for UnLike code.
    Thanks in advance.

    ReplyDelete

Post a Comment

Popular posts from this blog

This content database has a schema version which is not supported in this farm

Export all documents/Folders permissions to CSV using SharePoint PowerShell

Install WSP in SharePoint using Powershell