pnp / List-Formatting

List Formatting Samples for use in SharePoint and Microsoft Lists
https://pnp.github.io/List-Formatting/
MIT License
1.7k stars 822 forks source link

Naming Convention Checker #710

Closed joeyc773 closed 9 months ago

joeyc773 commented 9 months ago

What Kind of Sample (view or column format)

Column Format

Sample Description (what should it do)

My workplace has a naming convention for files which most people manage to fail to use. I'm trying to find a way to highlight if the naming convention hasn't been followed (this could be red text, a background, or even an icon).

The convention is "YYYYMMDD-[FileName]" e.g. 20230919-Request For Help.

So far I've been able to get it to check that it starts with "20" and at a minimum I'd like to check the following:

Ideally it would also maintain the 'action on click' that would normally be there before adding any formatting (see screenshot). image

Are you Willing to Help?

My JSON knowledge is extremely limited, but this is what I've managed so far: image

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "customRowAction": {
    "action": "defaultClick"
  },
  "txtContent": "@currentField",
  "style": {
    "padding-left": "14px",
    "cursor": "pointer"
  },
  "attributes": {
    "class": "=if(startsWith(@currentField, '20'), '', 'ms-fontColor-redDark')"
  }
}
tecchan1107 commented 9 months ago

Hi @joeyc773 . I have created a trial column formatting that checks the file name and displays the checked result as an icon.

image

filenamecheck

Here is the code I tried to create.

Sample Code - Version with custom card display (Click to open/close) ``` json { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "justify-content": "space-between", "align-items": "center", "width": "100%" }, "children": [ { "elmType": "div", "txtContent": "@currentField", "customRowAction": { "action": "defaultClick" }, "style": { "font-size": "14px" }, "attributes": { "class": "ms-Link ms-fontColor-neutralPrimary" } }, { "elmType": "div", "style": { "dispaly": "flex", "flex-direction": "row", "white-space": "nowrap" }, "children": [ { "elmType": "div", "style": { "display": "=if(((indexOf([$ContentTypeId],'0x0120')) == 0),'none', '')", "padding": "5px", "border-radius": "5px" }, "attributes": { "iconName": "=if(Number(substring(@currentField,0,8))&&(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20'),'CompletedSolid','StatusErrorFull')", "class": "='ms-fontSize-l '+if(Number(substring(@currentField,0,8))&&(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20'),'ms-fontColor-success ms-bgColor-success--hover','ms-fontColor-severeWarning ms-bgColor-severeWarning--hover')" }, "customCardProps": { "openOnEvent": "hover", "isBeakVisible": true, "directionalHint": "rightCenter", "formatter": { "elmType": "div", "style": { "padding": "15px 20px" }, "children": [ { "elmType": "div", "style": { "display": "flex", "flex-direction": "column", "width": "350px" }, "children": [ { "elmType": "div", "style": { "display": "flex", "flex-direction": "column", "margin-bottom": "10px" }, "children": [ { "elmType": "div", "txtContent": "File Name Check", "style": { "margin-bottom": "3px" }, "attributes": { "class": "ms-fontSize-l ms-fontWeight-bold" } }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "align-items": "center" }, "children": [ { "elmType": "div", "style": { "width": "23px" }, "attributes": { "iconName": "=if(Number(substring(@currentField,0,8)),'CompletedSolid','StatusErrorFull')", "class": "=if(Number(substring(@currentField,0,8)),'ms-fontColor-success','ms-fontColor-severeWarning')" } }, { "elmType": "div", "txtContent": "The file name starts with 8 numbers" } ] }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "align-items": "center" }, "children": [ { "elmType": "div", "style": { "width": "23px" }, "attributes": { "iconName": "=if(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20','CompletedSolid','StatusErrorFull')", "class": "=if(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20','ms-fontColor-success','ms-fontColor-severeWarning')" } }, { "elmType": "div", "txtContent": "The first 2 numbers of the file name are 19 or 20" } ] } ] }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "column", "margin-bottom": "10px" }, "children": [ { "elmType": "div", "txtContent": "File Name Example", "style": { "margin-bottom": "3px" }, "attributes": { "class": "ms-fontSize-l ms-fontWeight-bold" } }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "align-items": "center" }, "children": [ { "elmType": "div", "style": { "width": "23px" }, "attributes": { "iconName": "SquareShapeSolid" } }, { "elmType": "div", "txtContent": "20230921-WeeklyNews" } ] }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "align-items": "center" }, "children": [ { "elmType": "div", "style": { "width": "23px" }, "attributes": { "iconName": "SquareShapeSolid" } }, { "elmType": "div", "txtContent": "19981201-MonthlyNews" } ] } ] }, { "elmType": "div", "style": { "display": "flex", "flex-direction": "column" }, "children": [ { "elmType": "div", "txtContent": "Edit File Name", "style": { "margin-bottom": "3px" }, "attributes": { "class": "ms-fontSize-l ms-fontWeight-bold" } }, { "elmType": "div", "txtContent": "If you click in the frame below, you can edit the file name. After editing, press Enter.", "style": { "margin-bottom": "3px" } }, { "elmType": "div", "inlineEditField": "@currentField", "style": { "border": "1px solid", "border-radius": "3px", "padding": "3px" }, "children": [ { "elmType": "span", "txtContent": "@currentField" } ] } ] } ] } ] } } }, { "elmType": "div", "customRowAction": { "action": "openContextMenu" }, "style": { "padding": "5px", "font-size": "18px", "border-radius": "5px", "cursor": "pointer", "margin-left": "3px" }, "attributes": { "iconName": "More", "class": "ms-bgColor-neutralLighterAlt--hover" } } ] } ] } ] } ```
Sample Code - Version without custom card display (Click to open/close) ``` json { "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "children": [ { "elmType": "div", "style": { "display": "flex", "flex-direction": "row", "justify-content": "space-between", "align-items": "center", "width": "100%" }, "children": [ { "elmType": "div", "txtContent": "@currentField", "customRowAction": { "action": "defaultClick" }, "style": { "font-size": "14px" }, "attributes": { "class": "ms-Link ms-fontColor-neutralPrimary" } }, { "elmType": "div", "style": { "dispaly": "flex", "flex-direction": "row", "white-space": "nowrap" }, "children": [ { "elmType": "div", "style": { "display": "=if(((indexOf([$ContentTypeId],'0x0120')) == 0),'none', '')", "padding": "5px", "border-radius": "5px" }, "attributes": { "iconName": "=if(Number(substring(@currentField,0,8))&&(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20'),'CompletedSolid','StatusErrorFull')", "class": "='ms-fontSize-l '+if(Number(substring(@currentField,0,8))&&(substring(@currentField,0,2)=='19'||substring(@currentField,0,2)=='20'),'ms-fontColor-success','ms-fontColor-severeWarning')" } }, { "elmType": "div", "customRowAction": { "action": "openContextMenu" }, "style": { "padding": "5px", "font-size": "18px", "border-radius": "5px", "cursor": "pointer", "margin-left": "3px" }, "attributes": { "iconName": "More", "class": "ms-bgColor-neutralLighterAlt--hover" } } ] } ] } ] } ```

※I took "File is not a Folder" to mean "If it is a folder, do not check it". My apologies if I misunderstood.

tecchan1107 commented 9 months ago

I will close this since there seems to be no reply.

joeyc773 commented 9 months ago

Sorry, for some reason I didn't get notified of this so only just seeing it.

I can confirm this does what I needed and more! Thank you so much for this :)

joeyc773 commented 9 months ago

I have found one unforseen issue. We have links to records sites in the teams folders with this icon image

These files lose their title completely when this styling is applied..

tecchan1107 commented 9 months ago

We have links to records sites in the teams folders with this icon

I don't know exactly what it is, but it looks to me like the icon that appears when it is shared... I'll look into it after tomorrow.

These files lose their title completely when this styling is applied..

I just tried the code and it shows the file name with no problem. image

Is it possible that the code was not copied and pasted properly...?

joeyc773 commented 9 months ago

Hi, all other files work fine, it's specifically the shared folders with that icon. image

I've also noticed the Name column now does word wrapping, not a major issue but I know people in my team will ask if it's possible to change.