0

Can anyone explain this? I wrote this JSON back in June. It's to show birthdays for the current month. I have a simple list with a date field. To hide rows that are not in current month, I am comparing getMonth of the birth date versus @now.

Notice: my txtContent shows the result of the evaluation. When I publish the view - I'm getting no data. (Oddly - this worked in June and July.)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json",
  "schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
  "hideSelection": true,
  "hideColumnHeader": true,
  "rowFormatter": {
    "elmType": "div",
    "style": {
      "display": "=if(getMonth([$DateOfBirth])==getMonth(@now)), 'block', 'none')",      
      "white-space": "nowrap",
      "overflow": "hidden",
      "margin": "1px 4px 4px 1px"
    },
    "attributes": {
      "max-width": "150px"
    },
    "children": [
      {
        "elmType": "div",
        "style": {
          "text-align": "left",
          "font-size": "18px",
          "font-weight": 400,
          "font-family": "Segoe UI, Arial, Tahoma"
        },
        "children": [
          {
            "elmType": "span",
            "_comment_": "=getMonth([$DateOfBirth])+'/'+getDate([$DateOfBirth])+' - '+[$Title]+' - '+getMonth(@now)",
            "txtContent": "=getMonth([$DateOfBirth])==getMonth(@now)"
          }
        ]
      }
    ]
  }
}

When I preview - I get only TRUE entries (those with matching month values). When I save and refresh - it shows blank.

2
  • Why are using two schema? use only one which is required. What is _comment_? check if it is supported in JSON formatting. Also, try hard refresh (ctrl + f5) & clearing browser cache after saving the JSON. Aug 1, 2022 at 16:11
  • I pulled the schema definitions off a tutorial site. Wasn't sure if both were needed - doesn't hurt to have them referenced. Aug 2, 2022 at 17:27

2 Answers 2

1

There is a syntax error in your below expression:

"display": "=if(getMonth([$DateOfBirth])==getMonth(@now)), 'block', 'none')",

You have one additional ) after getMonth(@now).

Try using below JSON code, it should work for you:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json",
    "hideSelection": true,
    "hideColumnHeader": true,
    "rowFormatter": {
        "elmType": "div",
        "style": {
            "display": "=if(getMonth([$DateOfBirth])==getMonth(@now), 'block', 'none')",
            "white-space": "nowrap",
            "overflow": "hidden",
            "margin": "1px 4px 4px 1px"
        },
        "attributes": {
            "max-width": "150px"
        },
        "children": [
            {
                "elmType": "div",
                "style": {
                    "text-align": "left",
                    "font-size": "18px",
                    "font-weight": 400,
                    "font-family": "Segoe UI, Arial, Tahoma"
                },
                "children": [
                    {
                        "elmType": "span",
                        "_comment_": "=getMonth([$DateOfBirth])+'/'+getDate([$DateOfBirth])+' - '+[$Title]+' - '+getMonth(@now)",
                        "txtContent": "=getMonth([$DateOfBirth])==getMonth(@now)"
                    }
                ]
            }
        ]
    }
}
4
  • Ganesh - thanks for responding. I actually did not have the extra parenthesis in my code - I accidentally typed that when I posted my question. However - the issue seemed to be one of the platform not refreshed properly. This morning it was still behaving this way - and I noticed all of my August entries still appeared as "new". I edited a previous list entry to have a date starting in August, and suddenly the whole list started working. I reverted that entry back, and the list continued to work as expected. Aug 2, 2022 at 17:29
  • Great, glad it is working for you now! Just curious are you using any "platform" (which was not refreshed) for entering data in SharePoint? and what do you mean by "August entries still appeared as "new"? When you add new items in SharePoint list JSON formatting should work same for those items as well. Aug 3, 2022 at 5:19
  • Ganesh - when I opened the list through site contents, the list was showing my newest entries with the new "splash" icon, even 2 days later. No "platform" was used - these were entered directly into the list. It appears that SharePoint just had not "recognized" the entries until I went and made a change to an existing entry. (as if to say ... Sharepoint hadn't crawled the list data until that point, and somehow my edit made it wake up and smell the coffee.) I just chalk it up to another case of 80/20 rule with SharePoint. Again --- two words: Job Security. Aug 4, 2022 at 12:31
  • So - this is still not working properly. It didn't work all through August or September. When I added October entries, it worked --- once. It hasn't worked since. The code is simple - and I even added the "if" check to the row content to show that the true/false logic "works" properly. If I view All Items, then switch to my view - no data displays. I then switch to a similar view that shows all rows formatted - that works 100%. I can then switch to my month view and get data... until I refresh the view. Then - nothing shows again. I seriously think SharePoint sucks. Oct 19, 2022 at 15:37
0

So - it seems the issue was a glitch in the platform. The following day, when I previewed All Items, the entries that I submitted the day before still had the "new" moniker on them. I edited an existing entry from March to August, and suddenly the view started working. I changed the entry back to March - and the view continued to work as expected. My take on this - SharePoint wasn't refreshing properly. Makes no sense, and really supports my feeling of the 80/20 rule with this environment being wonky, ill-behaved, and occasionally unreliable.

With that said: job security. :-)

Your Answer

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

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