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);
very use for me
ReplyDeleteReputation.SetLike(ListID,ItemID,false);
ReplyDeleteHow i will use this in sharepoint 2013.
ReplyDeletethisItem.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.