2

My SharePoint list has a column for comments. It used to display a link to "View Comments" for every list item, though, even if there were no comments. Not helpful, since a user would have to click on every single link (one per list item) just to find out if there were any comments to see. I only want the View Comments link to appear if there are actually comments that exist for that list item.

I thought, I'll just use some conditional formatting and hide the label if there aren't any comments. I added this code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "style": {
    "display": "=if(@currentField == '', 'none', 'inherit')"
  }
}

At first I thought I was successful, because the links disappeared for the items without any comments:

enter image description here

The problem is, the links also disappeared for the list items that do have comments.

Now, even if I remove the extra JSON code, my "view comments" links are all gone, likely because my formatting has instructed the list not to display any text there, so it's overwritten the default "view comments" link with a null value.

So, I've gone from every list item having a link to view comments even if no comments exist, to no list item displaying a link to view comments, even if comments exist.

I have another problem and I'm sure that the two are related. I have a comments field within the form, but also have a separate comments pane, as shown below. They can both hold comments, but obviously it will be confusing to users if these two comment zones exist independently.

enter image description here

I need to make sure that I only have one method of commenting on list items, but I'm not sure which one to choose or how to get it to do what I want.

This is how my comments field is defined:

enter image description here

And, in my list settings, I have comments enabled:

enter image description here

So, my questions are:

  1. Should I either enable comments on the list (in the settings) or add a comments field as a column within the list, but not both? If so, I need to have a bit of help text visible (so users know not to include any personally-identifying information in their list item). Which method should I use to have one comment field with the help text visible to users?
  2. How can I make a link to the comments visible if there are comments present, but ensure that it doesn't show up if there are no comments to view?

Thanks!

1 Answer 1

2

Your format posted above is invalid. You must have an elmType and if you want to show a value you'll need to either provide it in txtContent or by adding children to your root element. For instance, your format could be fixed with something like this:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "display": "=if(@currentField == '', 'none', 'inherit')"
  }
}

So, if you are looking to have your own field (not what I would recommend) then formatting can still be used to conditionally show or hide based on the value of the item. However, multi-line text fields using rich text will nearly always have values (empty div) and there is no way to display the values of these fields in formatting without showing the underlying HTML included in the field's value.

If you really want to use your own commenting field and you want to evaluate it in formatting, you'll need to use plain text. But note that the version history of your items and key metadata like Modified and Modified By will be affected by comments. This will include personal information about the commentor as well. It's for these reasons that I recommend using the out of the box commenting.


If you use the out of the box list item commenting, the Title field will automatically show a button for items with comments with the number of comments in the tooltip:

enter image description here

Hovering over an item with no comments will prompt the user to add a comment:

enter image description here

Using this commenting will record the user who made the comment and date/time they made it. If enabled, users can even be @ mentioned. So if you are trying to keep comments from storing personal details about the commentor this isn't the way to go.

If you'd like to do custom formatting using the out of the box commenting you can get the number of comments for an item in formatting using this field name [$_CommentCount]. Here's a video demo of doing this exact thing I put together: https://www.youtube.com/watch?v=0X4AzvDDuIE

And here's the PnP Sample referenced: https://github.com/pnp/List-Formatting/tree/master/column-samples/comment-count

5
  • 1
    Thanks for the clear and complete answer. I should clarify that by "personal information" I didn't mean about the user (who, like you say, shows up all over the metadata), but that the list is to track COVID cases in schools and, in line with local legislation, I want to ensure that users don't include information about the person who's the subject of the reported case. So if I understand correctly, the best way forward is to leave commenting enabled in the list settings, delete my comments field, and use another mechanism to inform users of the guidelines?
    – Darryl
    Aug 31, 2021 at 20:04
  • Thanks for the clarification! Yeah I think you'll have an easier time using the list item commenting. You could use form formatting for either the Header or the Footer to create a clear warning about what is expected in the comments. Aug 31, 2021 at 22:35
  • Okay - deleted my "Comments" column and made sure that comments are enabled in the list settings, but in my list I don't see any icon displayed by the items that include comments. When I go to show/hide fields, the only ones not displayed are ID, Content Type, Attachments, DocIcon, Item Child Count, Folder Child Count, Label Setting, Retention Label, Retention Label Applied, Label Applied By, Compliance Asset ID and Item is a Record. How can I get the icon in the list if an item has comments?
    – Darryl
    Sep 1, 2021 at 15:20
  • The Title column default display should include that information (see screenshots above). I have found that sometimes a refresh of the list is necessary the first time an item has a comment. Alternatively, have you tried the sample linked above to show comment count? Sep 3, 2021 at 13:29
  • 1
    Okay, looks like a PBCAK error (problem between chair and keyboard)... I don't have prominent free-text field in this list so I hid the title column, hence no default comments icon is displayed. Your fabulous video, though, should help me to get what I need, and in any case you've covered off everything directly related to this question. Thanks!
    – Darryl
    Sep 3, 2021 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.