Like SharePoint item programmatically

18/02/2016 15:20

 make sure, you have referenced  Microsoft.Office.Serverfollowing.dll
   and using ReputationModel namespace 
   Microsoft.Office.Server.ReputationModel
 
 Like item
 Reputation.SetLike(list.ID.ToString(), itemId, true);
 
 Unlike item
  Reputation.SetLike(list.ID.ToString(), itemId, false);
 
 It's used in context of current user
 
 Find out if user has liked item
 
private bool WasItemLikedByCurrentUser(SPListItem item, SPUser currentUser)
{
 bool wasLiked = false;

 string likedByString = ListRepository.GetStringPropertyFromField(item, "LikedBy");
 //string likedByString = item["LikedBy"] as string;

 SPFieldUserValueCollection likedBy = new SPFieldUserValueCollection(item.Web, likedByString);
 SPFieldUserValue user = likedBy.OfType<SPFieldUserValue>().Where(u => u.User.LoginName == currentUser.LoginName).FirstOrDefault();
 if (user != null)
 {
  wasLiked = true;
 }
 return wasLiked;
}