Microsoft SharePoint

Formatting columns

Change News page to normal page (and vice versa)

  • Summary:
    • Column that determines whether a page is News or not: Promoted State
      • Values: 0 = Site Page, 1 = News page not yet published, 2 = News page
    • Set up
      1. Start by creating a new view in your SharePoint Site Pages library – base it on All Items and call it All News Posts
      2. In the view click Add column
      3. click Show or Hide columns
      4. choose Promoted State
      5. click Apply
      6. Hover of the heading of the Promoted State column
      7. select Column Settings
      8. select Format this column
      9. on the formatting pane that opens select Advanced mode
      10. Delete the default code displayed in the formatting box shown above and paste the code shown below there.
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "flex-wrap": "wrap",
    "display": "flex",
    "flex-direction": "row"
  },
  "children": [
    {
      "elmType": "div",
      "txtContent": "=if(@currentField == 0 ,'0 : Page' , if(@currentField == 1, '1 : News (Unpublished)' , if(@currentField == 2 , '2 : News','') ) )",
      "style": {
        "box-sizing": "border-box",
        "padding": "4px 8px 5px 8px",
        "display": "flex",
        "border-radius": "16px",
        "height": "27px",
        "align-items": "center",
        "white-space": "nowrap",
        "overflow": "hidden",
        "margin": "4px 4px 4px 4px",
        "border": "1px solid"
      },
      "attributes": {
        "class": "=if(@currentField == 0 ,'ms-fontColor-themePrimary ms-borderColor-themePrimary ms-bgColor-white' , if(@currentField == 1, 'ms-fontColor-themePrimary ms-borderColor-themePrimary ms-bgColor-themeLighter' , if(@currentField == 2 , 'ms-fontColor-white ms-borderColor-themePrimary ms-bgColor-themePrimary','') ) )"
      }
    },
    {
      "elmType": "div",
      "style": {
        "font-size": "18px",
        "cursor": "pointer",
        "padding": "10px",
        "border-radius": "50%",
        "display": "=if(@currentField == 0 , 'none' ,'')"
      },
      "attributes": {
        "iconName": "MoreVertical",
        "class": "ms-fontColor-themePrimary ms-bgColor-themeLighter--hover"
      },
      "customCardProps": {
        "openOnEvent": "click",
        "directionalHint": "rightCenter",
        "isBeakVisible": true,
        "formatter": {
          "elmType": "div",
          "txtContent": "Demote (Change to 0:Page)",
          "style": {
            "padding": "10px 20px 10px 20px",
            "cursor": "pointer"
          },
          "attributes": {
            "class": "ms-bgColor-themeLighter--hover"
          },
          "customRowAction": {
            "action": "setValue",
            "actionInput": {
              "PromotedState": "0"
            }
          }
        }
      }
    },
    {
      "elmType": "div",
      "style": {
        "font-size": "18px",
        "cursor": "pointer",
        "padding": "10px",
        "border-radius": "50%",
        "display": "=if(@currentField == 2 , 'none' ,'')"
      },
      "attributes": {
        "iconName": "MoreVertical",
        "class": "ms-fontColor-themePrimary ms-bgColor-themeLighter--hover"
      },
      "customCardProps": {
        "openOnEvent": "click",
        "directionalHint": "rightCenter",
        "isBeakVisible": true,
        "formatter": {
          "elmType": "div",
          "txtContent": "Promote (Change to 2:News)",
          "style": {
            "padding": "10px 20px 10px 20px",
            "cursor": "pointer"
          },
          "attributes": {
            "class": "ms-bgColor-themeLighter--hover"
          },
          "customRowAction": {
            "action": "setValue",
            "actionInput": {
              "PromotedState": "2"
            }
          }
        }
      }
    }    
  ]
}