Enable Like And Unlike Feature For SharePoint List Using JSOM

In this article, I would like to share the steps to enable Like/Unlike functionality in a SharePoint list and how to set Like/Unlike for a particular item using JSOM. In the previous article, we have seen how to activate the rating features in SharePoint list.

  • Like/Unlike is the Microblogging feature on SharePoint.
  • This feature is available in the reputation settings of list settings.
  • Mostly we are using this feature for a news web part, discussion, post etc..

Steps to enable the Like/Unlike feature in SP List

By default, SharePoint list doesn’t have this feature, using list settings we can enable this feature.

Follow the below steps to enable this feature,

Step 1

Open your SharePoint Site.

Step 2

Go to the site contents and then open the list where you want to enable this feature,

SharePoint

Step 3

Select list settings from top ribbon bar as shown below:

SharePoint

Step 4

On the list settings page select “Rating Settings”

SharePoint
Step 5

Enable the rating settings and check “Likes” option from rating settings as shown below,

SharePoint
Step 6

Then click “Ok” button to save the setting for your list,

SharePoint
Finally, Like/Unlike feature will be available for your list.

SharePoint

How to set Like/Unlike using JSOM

We have already gone through how to enable this feature for SharePoint lists, use the below code to set Like/Unlike using the JavaScript object model.

Steps to set like/unlike

Refer to the the below js files in your HTML page if you haven’t referred to them before. 

  1. <script src="../_layouts/15/sp.js" type="text/javascript"></script>  
  2. <script src="../_layouts/15/sp.runtime.js" type="text/javascript"></script>  
  3. <script src="../_layouts/15/reputation.js" type="text/javascript"></script>  
  4.   
  5. //likeDisplayText  Text will change dynamically based on the like/unlike  
  6. //itemId  Id for which id you need to set like  
  7. //listID  GUID for SharePoint list where the item is existing  
  8. var parameter = "LikePage(this, \"" + itemId + "\", \"" + listID + "\")";  
  9. <a href='#' onclick='" + parameter + "' class='LikeButton'>"+ likeDisplayText+"</a>  

On the Like page method write the below code,

  1. <script type="text/javascript">  
  2.   
  3.     function LikePage(obj, itemIDStr, listID) {  
  4.         SP.SOD.executeFunc('sp.js''SP.ClientContext'function () {  
  5.             // var clientContext = new SP.ClientContext.get_current();  
  6.             var currentSiteURL =”https://test.sharepoint.com”;  
  7.             var clientContext = new SP.ClientContext(currentSiteURL);  
  8.             console.log("site URL  : " + currentSiteURL);  
  9.             var like = false;  
  10.             var likeButtonText = $(obj).html();  
  11.             console.log("Like HTML: " + likeButtonText);  
  12.             likeButtonText = likeButtonText.toLowerCase();  
  13.             if (likeButtonText != "") {  
  14.                 if (likeButtonText.trim() == "like")  
  15.                     like = true;  
  16.                 console.log("Like : " + like);  
  17.                 var itemID = parseInt(itemIDStr);  
  18.                 EnsureScriptFunc('reputation.js''Microsoft.Office.Server.ReputationModel.Reputation'function () {  
  19.                     Microsoft.Office.Server.ReputationModel.  
  20.                     Reputation.setLike(clientContext,  
  21.                         listID,  
  22.                         itemID, like);  
  23.                     clientContext.executeQueryAsync(  
  24.                         function () {  
  25.                             if (likeButtonText.trim() == "like")  
  26.                                 $(obj).html("Unlike");  
  27.                             else  
  28.                                 $(obj).html("Like");  
  29.   
  30.                         }, function (sender, args) {  
  31.                             console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  32.                         });  
  33.                 });  
  34.             }  
  35.         });  
  36.     }  
  37. </script>  

Summary

In this article we have explored how to enable the Like/Unlike features in SharePoint lists and using  JSOM how to set the Like/Unlike for particular list items. You can use the preceding procedure for a Library also.