In this blog you will learn how to implement Like in SharePoint List Programmatically.
Here in this code we have explored about Likes fields and how users like any Custom List item.
Here in this code we have explored about Likes fields and how users like any Custom List item.
Operations I am performing here
- Get the List
- Get the List Items
- Get Site Users
- Get Liked Users And Likes Count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Web web = clientContext.Web
List list = web.Lists.GetByTitle("Products");
CamlQuery queryItem = new CamlQuery();
queryItem.ViewXml = "<View><Query><Where><Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">Spices</Value></Eq></Where></Query></View>";
ListItemCollection listItems = list.GetItems(queryItem);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
ListItem item = listItems[0];
clientContext.Load(web.SiteUsers);
clientContext.ExecuteQuery();
User user = web.SiteUsers[0];
foreach (User newUser in clientContext.Web.SiteUsers)
if (newUser.Title == "Arzoo Soni")
{
user = newUser;
break;
}
bool hasLiked = false;
FieldUserValue[] likedByUsers = (FieldUserValue[])item["LikedBy"];
List<FieldUserValue> newFieldUsers = new List<FieldUserValue>();
if (likedByUsers != null && likedByUsers.Length > 0)
foreach (FieldUserValue likedFieldUser in likedByUsers)
if (likedFieldUser.LookupValue == user.Title)
hasLiked = true;
if (!hasLiked)
{
if (likedByUsers != null)
foreach (FieldUserValue likedFieldUser in likedByUsers)
newFieldUsers.Add(likedFieldUser);
newFieldUsers.Add(FieldUserValue.FromUser(user.LoginName));
item["LikedBy"] = newFieldUsers;
item["LikesCount"] = Convert.ToInt32(item["LikesCount"]) + 1;
item.Update();
clientContext.Load(item);
clientContext.ExecuteQuery();
}
Thanks for reading!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Web web = clientContext.Web | |
List list = web.Lists.GetByTitle("Products"); | |
CamlQuery queryItem = new CamlQuery(); | |
queryItem.ViewXml = "<View><Query><Where><Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">Spices</Value></Eq></Where></Query></View>"; | |
ListItemCollection listItems = list.GetItems(queryItem); | |
clientContext.Load(listItems); | |
clientContext.ExecuteQuery(); | |
ListItem item = listItems[0]; | |
clientContext.Load(web.SiteUsers); | |
clientContext.ExecuteQuery(); | |
User user = web.SiteUsers[0]; | |
foreach (User newUser in clientContext.Web.SiteUsers) | |
if (newUser.Title == "Arzoo Soni") | |
{ | |
user = newUser; | |
break; | |
} | |
bool hasLiked = false; | |
FieldUserValue[] likedByUsers = (FieldUserValue[])item["LikedBy"]; | |
List<FieldUserValue> newFieldUsers = new List<FieldUserValue>(); | |
if (likedByUsers != null && likedByUsers.Length > 0) | |
foreach (FieldUserValue likedFieldUser in likedByUsers) | |
if (likedFieldUser.LookupValue == user.Title) | |
hasLiked = true; | |
if (!hasLiked) | |
{ | |
if (likedByUsers != null) | |
foreach (FieldUserValue likedFieldUser in likedByUsers) | |
newFieldUsers.Add(likedFieldUser); | |
newFieldUsers.Add(FieldUserValue.FromUser(user.LoginName)); | |
item["LikedBy"] = newFieldUsers; | |
item["LikesCount"] = Convert.ToInt32(item["LikesCount"]) + 1; | |
item.Update(); | |
clientContext.Load(item); | |
clientContext.ExecuteQuery(); | |
} |
Thanks for reading!!
No comments:
Post a Comment