SharePoint JSON Formatting – “Name cannot begin with the ‘=’ character”

Have you ever tried to apply column formatting to your SharePoint list views using code? If so, it’s likely you have come across this error and unfortunately there isn’t much information in the documentation on how the JSON Formatter string should be formatted.  Take for example this scenario:

Updating a view format using PnP PowerShell

PnP PowerShell has functionality for setting properties of a view using the command Set-PnPView

Updating the formatting on your list is done by passing in a JSON string to your Set-PnPView command using the CustomFormatter value property.

Set-PnPView -List "MyList" -Identity "MyViewName" -Values @{CustomFormatter = @'my-json-formatted-string'@}

Example JSON

Below is a basic example of applying a background color to a row on a view when the DueDate is less than now (date time).

{"schema":"https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json","additionalRowClass": "=if([$DueDate] <= @now, 'sp-field-severity--severeWarning', '')"}

Applying this format using PnP PowerShell

To apply this JSON script, you would use the following command

Set-PnPView -List "MyList" -Identity "MyViewName" -Values @{CustomFormatter = @'
{"schema":"https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json","additionalRowClass": "=if([$DueDate] <= @now, 'sp-field-severity--severeWarning', '')"}
'@
}

Running this command, you will likely receive the following error: Set-PnPView : Name cannot begin with the ‘=’ character, hexadecimal value 0x3D.

The Fix:

The reason you are seeing this error is because in the JSON itself you need to encode some of the values if you are using operators. What I mean by that is, if you are using &&, or operators in formulas such as “>=” or “<=“, you need to use their encoded values instead. In our example, we were using [$DueDate] <= @now. In order to apply this to our view, we need to encode “<=” into  “&lt;=” and the formula will work.

Below is the following command with a working JSON formatter value.

Set-PnPView -List "MyList" -Identity "MyViewName" -Values @{CustomFormatter = @'
{"schema":"https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json","additionalRowClass": "=if([$DueDate] &lt;= @now, 'sp-field-severity--severeWarning', '')"}
'@
}

 

Hope this helps you!

Rendering multi-value Choice fields vertically using JSON Column Formatting

Recently I was perusing a SharePoint forum post and a member asked if there was a way to change the visual representation of a multiple value choice field in SharePoint. My first thought was to use JSON Column Formatting.

The problem

By default, SharePoint renders a multiple value choice field as a single string in a row, and renders the HTML as a single value in a div.

HTMLChoiceField

One question, if you aren’t familiar with JSON Column Formatting is how would we render these items as new lines if they represented as a single value in the HTML. You’d probably first go and see if you could split on the commas “,”… but unfortunately column formatting does not support a split function.

Introducing ‘forEach’

One feature that column formatting does have is the forEach function. This is an optional property that allows an element to duplicate itself for each member of a multi-valued field. To loop through multi-value fields we’d use the following format

"iteratorName in @currentField" or "iteratorName in [$FieldName]"

Once you’ve implemented the forEach, you have access to each member you are looping through by using the iterator name. For example, if we loop through @currentField using the following formula: "iteratorName in @currentField" we can gain access to each record using [$iteratorName].

Putting it into action

Now that we know we can loop through multi-choice fields, all we need to do is come up with a JSON column formatter which creates each record on it’s own row. See the below JSON object.

We are using the forEach property to loop through each choice value in the currentField. For each record, we set the textContext equal to the choice record, and then we just style the div to be displayed block and 100%

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "debugMode": true,
  "elmType": "div",
  "children": [
    {
      "elmType": "div",
      "style": {
        "display": "block",
        "width": "100%"
      },
      "forEach": "choice in @currentField",
      "txtContent": "[$choice]"
    }
  ]
}

The end result turns the original choice field, to be rendered like so:

ChoicesHTML

MultipleLineChoice