Back to column formats
Column formattingSharePoint Online

Like/Unlike List Item

Demonstrates how to like/unlike a list item.

Like/Unlike List Item formatting preview
Sample preview · View original animation

Copy. Apply. Make it yours.

Get this format

1 definition

Review the requirements below, then paste this JSON into the matching formatting panel. Show me where.

Use your list's internal column names and replace any tenant-specific URLs or flow IDs. Formatting is presentation, not a way to secure data.

Column formatting
Download JSON
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "div",
      "style": {
        "font-size": "18px",
        "padding": "2px 12px",
        "cursor": "pointer"
      },
      "attributes": {
        "class": "ms-fontColor-themePrimary",
        "title": "=if(indexOf([$LikedBy.email], @me) > -1 , 'Unlike', 'Like')"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "LikesCount": "=if(indexOf([$LikedBy.email], @me) > -1, [$LikesCount] - 1, [$LikesCount] + 1)",
          "LikedBy": "=if(indexOf([$LikedBy.email], @me) > -1, removeFrom([$LikedBy.email], @me), appendTo([$LikedBy.email], @me))"
        }
      },
      "children": [
        {
          "elmType": "span",
          "attributes": {
            "iconName": "=if(indexOf([$LikedBy.email], @me) > -1 , 'Dislikesolid', 'Likesolid')"
          }
        },
        {
          "elmType": "span",
          "style": {
            "margin-left": "5px"
          },
          "txtContent": "[$LikesCount]"
        }
      ]
    }
  ]
}

From the contributor

How to use this sample

Summary

This sample demonstrates how to like/unlike a list item. This sample uses the setValue customRowAction to update the number and person field.

screenshot of the sample

View requirements

This format can be applied to any column type (its value is ignored). In addition, below fields needs to be defined:

Type Internal Name Required Comments
Number LikesCount No Default value = 0
Person LikedBy No Allow multiple selections

Sample

Solution Author(s)
generic-like-unlike.json Ganesh Sanap

Version history

Version Date Comments
1.0 November 27, 2021 Initial release

Disclaimer

THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.