LIKE or UNLIKE a list item programmatically

If Likes are enable in the Rating Settings of a list then users can like/unlike a list item using the UI. A list item can also be liked/unliked programmatically.
There are to ways to like/unlike an item programmatically.

1.Using Reputation class.

This is the recommended way.Reputation class is used to set Likes and Ratings for list items.
To like an item:

    Reputation.SetLike(listID, itemID, true);

To unlike an item:

	Reputation.SetLike(listID, itemID, false);

2.Directly by updating the item’s LikesCount and LikedBy fields.

    SPFieldUserValueCollection likedBy = new SPFieldUserValueCollection(web, item["LikedBy"].ToString());

	SPUser user = web.EnsureUser("YOURDOMAIN\\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);

If you increase LikesCount without adding new user in LikedBy field the item will show the increased number of likes temporary but with the first like/unlike from a user using the UI the number of likes will be automatically corrected to reflect the number of users that have liked the item.

To unlike decrease LikeCount field value and remove the user from LikedBy.

Workflow progress column missing in a document library

Problem:

Workflow progress column is missing even though workflow instances have been started for many documents.

Case:

There is a document library with a workflow associated with its content type. Even though there have been workflow instaces started there is no workflow progress column created for the list.
The missing column doesn’t affect the work of the workflow itself. The workflow can be started, completed and it is working without problems, only the progress column is not available.

Elaboration:

Workflow progress column should be created when the first workflow instance is stared on a document. It turns out that if for some reason has failed to be created when the first workflow instance is started then it will not be created with next new instances of the workflow even if the problem that caused the failure the first time is fixed.

In my case the workflow progress column is not created because of the lookup threshold. Workflow progress column is lookup column. If the default view has more lookup columns than the lookup threshold (by default 8) then the creation of the workflow progress column fails.

By removing lookup columns form the view or increasing the lookup threshold in Central Administration then start another workflow instance the problem was not fixed. I had to remove the original workflow and add another identical workflow to the list. When starting the first instance of the new workflow the workflow progress column was created successfully.

Solution:

To fix the problem of missing workflow status column follow the steps below:

  1. Remove lookup columns for the list’s default view so its number is below the lookup threshold for that web application, or increase the lookup threshold for that web application in Central Administration.
  2. Remove the workflow from the content type when there are no running instances
    Important: With this action workflow history is lost for this workflow
  3. Add new workflow to the content type. Start a workflow instance on a document so the workflow progress column is created.
%d bloggers like this: