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
Post a Comment